Print the sum of all digits

View previous topic View next topic Go down

Print the sum of all digits

Post by Admin on Fri Jun 26, 2015 2:15 pm

Write a program to print the sum of all the digits of a given number. The sum must be between 0-9 only and don't use array!!!

Ex. I/P: 423
O/P: 9

I/P: 478
O/P: 1

Admin
Admin

Posts : 22
Points : 5354
Join date : 2015-06-24
Age : 23

http://csenpsb.createforum.in

Back to top Go down

Re: Print the sum of all digits

Post by vinoveera on Mon Jun 29, 2015 12:34 am

POST THE ANS ADMIN

vinoveera

Posts : 7
Points : 5304
Join date : 2015-06-27

Back to top Go down

Answer to the program

Post by Admin on Mon Jun 29, 2015 5:50 pm

#include<iostream.h>
void main()
{
int num,rem,sum,temp;
cout<<"Enter a number to print sum of the digits: ";
cin>>num;
sum=num;
while(sum>9)
{
sum=0;
while(num>0)
{
rem=num%10;
sum=sum+rem;
num=num/10;
}
num=sum;
}
cout<<"The sum is "<<sum;
}

Admin
Admin

Posts : 22
Points : 5354
Join date : 2015-06-24
Age : 23

http://csenpsb.createforum.in

Back to top Go down

Re: Print the sum of all digits

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum