sahajjain01

Display the sum of a number's digits.

Aug 21st, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.31 KB | None | 0 0
  1. //Program to display the sum of a number's digits.
  2. #include<iostream.h>
  3. #include<conio.h>
  4. void main()
  5. {
  6.     clrscr();
  7.     int a,b,c;
  8.     cout<<"Enter a number to display sum of it's digits: ";
  9.     cin>>a;
  10.     b=a%10;
  11.     while(a!=0)
  12.     {
  13.         a=a/10;
  14.         c=a%10;
  15.         b=b+c;
  16.     }
  17.     cout<<"The sum of the digits is: "<<b;
  18.     getch();
  19. }
Advertisement
Add Comment
Please, Sign In to add comment