shawon13 -4 years ago 57
C Question

# Armstrong number program produces wrong results when compiled using Codeblocks and MinGW(32 bit)

I wanted to write a program to figure out whether a number is an armstrong number or not. I wrote a function for this purpose that'll return 1 if the number is an armstrong number and 0 if otherwise.

I used the following code for the function:

``````int isArmsNum(int x)
{
int m, r = 0, d, y;

y = x;

while (x > 0)
{
m = x % 10;
r = r + pow(m, 3);
x = x / 10;
}

if (r == y)
{
d = 1;
}
if (r != y)
{
d = 0;
}

return d;
}
``````

Now the problem is when I try to check the program for 153(which is an armstrong number) the program returns 0 and it return 1 only if x = 1. I compiled the same code in Visual Studio 2013, and it was working fine.

I decided to do some debugging on Codeblocks(13.12), and I found that,
inside the while loop, (checking with x = 153)

• after the first iteration, m = 3, r = 27, x = 15 // fine for now

• after the second iteration, m = 5, r = 151, x = 1 // here is the problem. r should be 152 as 27+(5^3) = 152

so, when the loop terminates, r has the value where it should've been 153.

What could possibly be the problem here? Any ideas?

[Codeblocks 13.12 with GCC 4.7.1-32 bit MinGW]

[My PC is running on Windows 8.1 Pro 64 bit]

[Tested the code on other IDE's, worked. Also tried out on OS X using GCC via terminal and in Xcode, worked]