This question already has an answer here:
- Modulo operator with negative values 2 answers
For example the below code runs fine when we enter a positive number but it gives different answer when we enter the same number with the negative sign.I know that we can convert the number to positive be abs() or by any other method but I want to know what is going on with the flow and execution of the program.
#include <iostream>
using namespace std;
int main(){
int sum=0, rem;
unsigned int n;
cout<<"Enter the number : n";
cin>>n;
while(n!=0){
rem=n%10;
sum=sum+rem;
n=n/10;
}
cout<<"addition of digits is : n"<<sum;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire