Write a program that reads an integer between 0 to 999
and adds all the digits in the integer.
#include <stdio.h>
#include <conio.h>
int main()
{
int count = 0, num = 0;
int reminder = 0, sum = 0;
int stop = 0;
while (stop != -1)
{
printf("Enter an integer: ");
scanf_s("%d", &num);
printf("\n After operation:\n");
printf("reminder num\n");
printf("--------- ---\n");
while(num != 0)
{
reminder = num % 10; /* get the reminder (digits) by dividing by 10*/
sum = sum + reminder; /* sum up the reminder*/
num = num / 10;
printf("%d %d\n", reminder, num); // let see current value of num and
// reminder...
}
printf("\n");
printf("The sum of digits is %d\n", sum); /* print the sum of the digits */
sum = 0;
printf("More? -1 to stop, other to continue: ");
scanf_s("%d",&stop);
}
return 0;
}
#include <conio.h>
int main()
{
int count = 0, num = 0;
int reminder = 0, sum = 0;
int stop = 0;
while (stop != -1)
{
printf("Enter an integer: ");
scanf_s("%d", &num);
printf("\n After operation:\n");
printf("reminder num\n");
printf("--------- ---\n");
while(num != 0)
{
reminder = num % 10; /* get the reminder (digits) by dividing by 10*/
sum = sum + reminder; /* sum up the reminder*/
num = num / 10;
printf("%d %d\n", reminder, num); // let see current value of num and
// reminder...
}
printf("\n");
printf("The sum of digits is %d\n", sum); /* print the sum of the digits */
sum = 0;
printf("More? -1 to stop, other to continue: ");
scanf_s("%d",&stop);
}
return 0;
}
No comments:
Post a Comment