Hi!
I was asked to create a program that will get numbers from the user.
The numbers should always be positive and disregards the negative sign.
The numbers will then be converted to digits.
I need to print it in the right order as it looked like the number entered by the user.
I should also get the sum of the digits and tell if it is divisible by 3.
If the input of the user is 0 the program should end.
So here's my code:
#include <stdio.h>
#include <stdlib.h>
int main () {
int input, individual, reverse=0;
do { printf ("Please Enter a number: "); scanf ("%d", &input);
if (input!=0) { int sum=0, i=1;
printf ("The Digits are ");
while (input!=0) { input = abs (input); individual= input%10; sum += individual; input/=10; ++i; printf ("%d ", individual); }
printf ("\nThe Sum of the Digit(s) is %d\n", sum);
if (sum % 3 == 0) { printf ("This Number is divisible by 3!\n"); }
printf ("\n"); }
else if (input == 0) { return 0;
system ("pause");
}
} while (1);
system ("pause");
}
Sample run:
Please Enter a number : 123
The Digits are 3 2 1
The sum of the digits is 6
this number is divisible by 3!
Please Enter a Number
What I wanted it to output is :
Please Enter a number : 123
The Digits are 123
The sum of the digits is 6
this number is divisible by 3!
Please Enter a Number