answersLogoWhite

0


Best Answer

(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.)

User Avatar

Wiki User

16y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

// 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;

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

#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

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

int n; // we want to find the sum of digits of n

int sum = 0;

while(n != 0 ) {

sum += n % 10;

n /= 10;

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

for (sum=0; n!=0;) sum+=n%10, n /= 10;

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Sum of digits of a number equals to the given number?
Write your answer...
Submit
Still have questions?
magnify glass
imp