C Program to find sum of digits of n digit number

 Program to find sum of digits of n digit number

C Program to find sum of digits of given n digit number using while loop

Program:


#include <stdio.h>
 
int main()
{
   int n, t, sum = 0, remainder;
 
   printf("Enter an integer\n");
   scanf("%d", &n);
 
   t = n;
 
   while (t != 0)
   {
      remainder = t % 10;
      sum       = sum + remainder;
      t         = t / 10;
   }
 
   printf("Sum of digits of %d = %d\n", n, sum);
 
   return 0;
}



Output:

output of Program to find sum of digits of n digit number


Flowchart:
flowchart of program to find sum of digits of n digit number






Guest post by: Ruturaj Shivalkar

No comments:

Post a Comment

Feel free to ask us any question regarding this post