sir i have written same code as taught in the video lecture and it’s not working
My code is not working
#include
using namespace std;
int main(){
int n;
cin>>n;
int sum=0;
while (n>0){
int last_digit=n%10;
sum = sum + last_digit;
n=n/10;
cout<<“sum of digits”<<sum<<endl;
return 0;
}
}
@Anku47
You have wrote cout<<“sum of digits”<<sum,endl; and return 0; inside while() loop.
These two line should be outside the scope of while() loop. Cut them and paste them outside while loop.
1 Like