C Programming Tutorial

Advertisements

Write program to show sum of digits of a number in c



       #include<stdio.h>
       #include<conio.h>
       void main()
       {
              int num,m,sum=0; 
              clrscr();

              printf("\nEnter any number : "); 
              scanf("%d",&num); 

              while(num>0)
              {
                   m = num % 10;                   
                   sum = sum + m;
                   num = num / 10;
                }
              
              printf("\nThe sum is : %d",sum);      

              getch();
       }

   Output : 

              Enter any number : 3275 
              The sum is : 17




Advertisement