Sum of digits program in C

Sum of digits program in C


Prerequisites:- While loop in CFor loop in C

The sum of digits of the number 54321 = 5+4+3+2+1 = 15

Sum of Digits in C using the while loop

 #include<stdio.h>
 int main()
 {
     int number,lastDigit,sum=0;

     printf("Enter any number: ");
     scanf("%d",&number);

     while(number!=0)
     {
         lastDigit=number%10;
         sum += lastDigit;
         number/=10;
     }

     printf("Sum of digits =  %d\n",sum);

     return 0;
 }