Pages

Write a program that reads an integer between 0 – 999 and adds all the digits in the integer.

Tuesday, 23 February 2016
#include <stdio.h>
int main()
{
  int count = 0, num = 0, remainder = 0, sum = 0, stop = 0;
  while(stop != -1)
  {
  printf("Enter an integer: ");
scanf_s("%d", &num);

  printf("\nAfter operation:\n");
  printf("remainder num\n");
  printf("--------- ---\n");
 
               while(num != 0)
{
       remainder = num % 10;
       sum = sum + remainder;
       num = num / 10;
       printf("%d %d\n", remainder, num);
  }
  printf("\n");
printf("The sum of digits is %d\n", sum);
sum = 0;
  printf("More? -1 to stop, other to continue: ");
  scanf_s("%d",&stop);
  }
 return 0;
}
Read more ...