C Programming Tutorial

Advertisements

Write program to show sum of digits of a number in C++



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

              cout<<"\nEnter any number : "; 
              cin>>num; 

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

              getch();
       }

   Output : 

              Enter any number : 3275 
              The sum is : 17




Advertisement