Write a C program to calculate sum of digits of a given input number.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,sum;  //n=number, r=remainder, sum=sum of digits

   //accept number from user
   printf("\nEnter number: ");
   scanf("%d",&n);

   sum=0; //initialize value of sum=0

   //separate each digit from number and add it in sum variable
   while(n!=0)
   {
    r=n%10;
      sum=sum+r;
      n=n/10;
   }

   //printing result
   printf("sum is : %d",sum);
   getch();
}

0 Comments