Print the sum of all digits
2 posters
Page 1 of 1
Print the sum of all digits
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
Ex. I/P: 423
O/P: 9
I/P: 478
O/P: 1
Re: Print the sum of all digits
POST THE ANS ADMIN
vinoveera- Posts : 7
Points : 22699
Join date : 2015-06-27
Answer to the program
#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;
}
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;
}
Similar topics
» Print number in words
» Print fibonacci series
» Print second largest number
» Print palindrome number
» Print fibonacci series
» Print second largest number
» Print palindrome number
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|