C Program to find Sum of Digits of a Number

Sum of digits in C : This C program uses while loop to find and print sum of digits of a number. It extracts the last digit of a number and adds it to the result until there are no digits left to extract.

C program to find sum of digits of a number

/* Aim: Write a program to calculate sum of digits of a number print number of digits */

#include<stdio.h>

int main()
{
 int r,num,sum=0,digit=0,original;

 printf("\n Enter any number:- ");
 scanf("%d",&num);

 original=num;

 while(num>0)
 { 
 r=num%10;
 sum+=r;
 num/=10;
 ++digit;
 }

 printf("\n The sum of digits of %d is %d and %d digits are persent in the number \n \n",original,sum,digit);

 return 0;
}

/* Output of above code:-

 Enter any number:- 1234

 The sum of digits of 1234 is 10 and 4 digits are persent in the number 

*/

Related C Programs

Program to count number of digits in C