Tilak Raj
Tilak Raj

Reputation: 1499

Modulus of same number returns different results

int a=032302;
cout<<a%10<<endl; // output 6
int b=32302;
cout<<b%10<<endl;  // output 2

I was trying to get the unit's place of a number but while coding i found a weird thing, the first and the second no are technically same, however they both output different results.

The first one returns 6 while the second one 2 , am i missing something here?

Upvotes: 0

Views: 63

Answers (2)

Considering the fact that

int a = 032302; 

and

int b = 13506;

are holding the same integer value since variable a is init as octal literal

then is correct that

a%10 returns 6 same as b%10 returns 6

Upvotes: 1

JohanL
JohanL

Reputation: 6891

Starting a numeral with 0 (zero) in c/c++ means it is an octal (base 8) number. Thus 032302 is 13506 in decimal notation. Hence, the last digit is 6 and that is what you get from your modulus operation.

Upvotes: 3

Related Questions