(I'm assuming base 10 math) You could cheat. Convert the number to a string with no leading zeros. Capture the left most and right most characters of the string. Convert captured string variables back to integers. Add the integers. (Note: Consider a base string having a string length of 0 or 1.)
Chat with our AI personalities
// returns the sum of the digits of num
int sumDigits(int num) {
int sum = 0;
int currentDigit;
// four step algorithm:
// step 1: loop until we have no digits left to process
while(num > 0) {
// step 2: find out the rightmost digit
currentDigit = num % 10;
// step 3: add the rightmost digit to our sum
sum += currentDigit;
// step 4: remove the rightmost digit from num
num /= 10;
}
return sum;
}
#include
#include
using namespace std;
int main()
{
int n,m,sum=0;
cout<<"Enter n:";
cin>>n;
m=n;
while(n>0)
{
sum+=n%10;
n=n/10;
}
if(sum==m)
cout<<"Equal :";
else
cout<<"Not equal";
return 0;
}
supermanhelp.com
int n; // we want to find the sum of digits of n
int sum = 0;
while(n != 0 ) {
sum += n % 10;
n /= 10;
}
5
It appears that only single digit numbers work (0 thru 9)
Well, it's very hard to write a flowchart in text, so I'll give you some pseudo code instead. int number = the given number int sum = 0 loop while number is not 0 sum = sum + (number mod 10) number = number / 10
Peterson Number:145 = 1! + 4! + 5!number=sum of (factorials of digits)
# includevoid main(){int no,rem=0,sum=0,n; /*declaration*/printf("Enter 2 digit number:");scanf("%d",&no);for(n=1;n