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

17y ago

Still curious? Ask our experts.

Chat with our AI personalities

RafaRafa
There's no fun in playing it safe. Why not try something a little unhinged?
Chat with Rafa
JudyJudy
Simplicity is my specialty.
Chat with Judy
BeauBeau
You're doing better than you think!
Chat with Beau
More answers
User Avatar

Wiki User

16y 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;

}

User Avatar

User Avatar

Wiki User

15y 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

User Avatar

User Avatar

Wiki User

16y ago

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

int sum = 0;

while(n != 0 ) {

sum += n % 10;

n /= 10;

}

User Avatar

User Avatar

Wiki User

15y ago

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

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