I earlier posted a topic to help me do this but i didnt have a code. I have finally completed this with a help of a local friend but i dont understand the logic of it.
#include <cstdlib>
#include <iostream>
usingnamespace std;
int main()
{
int number;
int sum=0;
cout << "Enter an integer:";
cin >> number;
while(number>0){
sum=sum+number%10; // sum + 2.1%10 is supposedly the output which is 3? How so
number=number/10; // number=21/10 which is 2.1
}
cout << "The sum is" << sum << endl;
system("PAUSE");
return (0);
}
when I run the code if I input integer for example 21 it will give me 3 as a sum which is correct.
I dont understand how the sum will be 3 if its (0=sum+2.1%10) because 2.1%10 doesnt equal to 3 or does it??