First
u have to understand the question and think how to get into the
question. Here are a few steps to follow.
- To get the digits of a number.
- Adding each digit to a variable
- Print that digit
- Finally print the sum of digits.
Generally,
to get the digits of a number, we use divison( / ) or modulo (%)
operator. I hope, by using this, one can get the digits of a number.
Here is a code for the above question.
#include<iostream>
using
namespace std;
int
main()
{
int
a,b,c=0;
cout<<"enter
the number: "; // print reverse of
number
cin>>a;
cout<<"reverse
number is : ";
while(a!=0)
{
b=a%10;
cout<<b;
a=a/10;
c=c+b;
}
cout<<
"\nsum of digits is: "<< c; //
sum of the digits of number
return
0;
}
Output:
CLICK HERE
For
this code, if u give input as 7400 then the corresponding output will
be 0047 , that is equal to 47. So now U try to print in this format.