1) In main() function you ask a user to enter some positive number. If a number
ID: 3553043 • Letter: 1
Question
1) In main() function you ask a user to enter some positive number. If a number is not positive you terminate the program.
2) Otherwise you call a separate function from main() that takes this number and does the following:
prints digits of this number;
and returns/prints the sum of digits.
guidelines:
1) You write a separate function that finds digits and the sum
2) You use a while loop inside your function
3) You call your function from main() for some input from the user (input has to be positive) 4) You get digits printed as a result and sum of the digits (see expected output below)
Example:
Enter a number: 567
The digits are:
5
6
7
The sum of digits is: 18
Explanation / Answer
#include<iostream>
void sum(int x)
main()
{
int n;
cout<<"enter the number";
cin>>n;
if(n>0)
{
sum(n);
}
else
{
exit;
}
void sum(int x)
{
int i=0
int sum=0
int r,a[10],t;
while(r!=0)
{
r =n%10;
a[i]=r;
i++;
sum=sum+r;
}
cout<<"the sum is"<<sum<<"the digits are"
for(i=0;i<10;i++)
cout<<a[i]
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.