Hey, can you help me with this? I know I should add more variables but I still dont know where. You are given a number and you have to find the sum of the numbers that consist of that number, for ex. if you are given 177, the answer should be 15. I know it's pretty easy but I still can't crack this. I dont know how to complete the division part.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
usingnamespace std;
int main (){
float n=1, sum=0; // n-number(that will be given)
while(n>0){
cin>>n;
n=n%100;
n=n/ ; //???
sum=sum+n;
}
cout<<sum;
return 0;
#include <iostream>
usingnamespace std;
int main (){
int n=1, sum=0; // n-number(that will be given)
while( n != 0){
cout << "enter: ";
cin>>n;
sum+=n%100;
n -=100 * n;
sum+=n%10;
n-= 10*n;
sum+=n%1;
n-= 1*n;
}
cout<<sum;
return 0;
}
Code may not be generic, but you can probably fix that.
Btw, sum must be an integer, because your numbers won't divide completely.
So 177%100 = 1.77 or 1
77%10 = 7.7 or 7
7%1 = 7 or 7.
Now I give you the task to incorporate loops to make the program more generic, and allow any number work. This program works for numbers 999 and below. Make it work for any.