C
Camellia
hi all,
I wrote a "table of powers" program and made the 5th power the highest,
but when I tried to run the program I found a problem. Here's the
output:
Integer Square power 3rd power 4th power 5th
------- ------ --------- --------- ---------
1 1 1 1 1
2 4 16 256 65536
3 9 81 6561 43046721
4 16 256 65536 0
5 25 625 390625 -2030932031
6 36 1296 1679616 -683606016
7 49 2401 5764801 -1526366847
8 64 4096 16777216 0
9 81 6561 43046721 -501334399
10 100 10000 100000000 1874919424
and the code:
<code>
#include <stdio.h>
#define MAX 10
int power(int, int);
int main(void)
{
long int original = 1, p2, p3, p4, p5;
int i;
printf("%7s%15s%15s%15s%15s\n%7s%15s%15s%15s%15s\n",
"Integer", "Square", "power 3rd", "power 4th", "power
5th",
"-------", "------", "---------", "---------",
"---------");
for (i = 1; i <= MAX; ++i) {
original = i;
p2 = power(original, 2);
p3 = power(original, 3);
p4 = power(original, 4);
p5 = power(original, 5);
printf("%7d%15d%15d%15d%15d\n",
original, p2, p3, p4, p5);
}
}
int power(int a, int power)
{
int i;
for (i = 1; i < power; ++i)
a = a*a;
return a;
}
</code>
Why does the "5th power" not work properly? Is the problem with the
"int" declaration?
Any suggestions are appreciated.
I wrote a "table of powers" program and made the 5th power the highest,
but when I tried to run the program I found a problem. Here's the
output:
Integer Square power 3rd power 4th power 5th
------- ------ --------- --------- ---------
1 1 1 1 1
2 4 16 256 65536
3 9 81 6561 43046721
4 16 256 65536 0
5 25 625 390625 -2030932031
6 36 1296 1679616 -683606016
7 49 2401 5764801 -1526366847
8 64 4096 16777216 0
9 81 6561 43046721 -501334399
10 100 10000 100000000 1874919424
and the code:
<code>
#include <stdio.h>
#define MAX 10
int power(int, int);
int main(void)
{
long int original = 1, p2, p3, p4, p5;
int i;
printf("%7s%15s%15s%15s%15s\n%7s%15s%15s%15s%15s\n",
"Integer", "Square", "power 3rd", "power 4th", "power
5th",
"-------", "------", "---------", "---------",
"---------");
for (i = 1; i <= MAX; ++i) {
original = i;
p2 = power(original, 2);
p3 = power(original, 3);
p4 = power(original, 4);
p5 = power(original, 5);
printf("%7d%15d%15d%15d%15d\n",
original, p2, p3, p4, p5);
}
}
int power(int a, int power)
{
int i;
for (i = 1; i < power; ++i)
a = a*a;
return a;
}
</code>
Why does the "5th power" not work properly? Is the problem with the
"int" declaration?
Any suggestions are appreciated.