+ 2

Where i am going wrong help

#include <iostream> using namespace std; int main() { int n,r,sum=0; cout<<"Enter a number "; cin>>n; while (n>0){ r=n%10; sum=sum+n; n=n/10; } cout<<"\nthe sum of your digit is:"<<sum; return 0; }

1st Jan 2018, 5:34 PM
Dipankar Verma
Dipankar Verma - avatar
2 Antworten
+ 3
sum=sum+n <-- should be r not n if you want the sum of the digits.
1st Jan 2018, 5:41 PM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Oh my bad Thank you by the way
1st Jan 2018, 5:42 PM
Dipankar Verma
Dipankar Verma - avatar