Logical Analysis and Programming
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Print the sum of all digits

2 posters

Go down

Print the sum of all digits Empty Print the sum of all digits

Post by Admin 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 : 22749
Join date : 2015-06-24
Age : 29

https://csenpsb.board-directory.net

Back to top Go down

Print the sum of all digits Empty Re: Print the sum of all digits

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

POST THE ANS ADMIN

vinoveera

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

Back to top Go down

Print the sum of all digits Empty Answer to the program

Post by Admin 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 : 22749
Join date : 2015-06-24
Age : 29

https://csenpsb.board-directory.net

Back to top Go down

Print the sum of all digits Empty Re: Print the sum of all digits

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

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