Hey guys, I'm building a program in C that can get powers of 2. The user inputs the value of n, and the program calculates 2^n.
Here's the code
The problem comes when I input 100
I've tried to modify the program, but so far, no luck...
It has to be coded entirely in ANSI C. Any ideas on how to increase the precision? The maximum value of N has to be 256 (256 bits, I imagine).
EDIT: I edited the code, look at it
The problem is that, when I insert 63, it shows a negative number, and when inserting 64 and beyond, it prints 0.
Edited by Icekilla - 10/31/12 at 6:46pm
Here's the code
Code:
#include <stdio.h>
double power(int n)
{
int q;
double c=2;
for (q=n; q>1; q--) {c=2*c;}
return c;
}
int main()
{
int n;
double output;
scanf("%i",&n);
output=power(n);
printf("%.0lf",output);
return 0;
}
The problem comes when I input 100
Quote:
What I am getting 1,267,650,600,228,229,400,000,000,000,000
What I should get 1,267,650,600,228,229,401,496,703,205,376
What I should get 1,267,650,600,228,229,401,496,703,205,376
I've tried to modify the program, but so far, no luck...
It has to be coded entirely in ANSI C. Any ideas on how to increase the precision? The maximum value of N has to be 256 (256 bits, I imagine).
EDIT: I edited the code, look at it
Code:
#include <stdio.h>
long long power(int n)
{
int q;
long long c=2;
for (q=n; q>1; q--) {c=2*c;}
return c;
}
int main()
{
int n;
long long output;
scanf("%i",&n);
output=power(n);
printf("%lld",output);
return 0;
}
The problem is that, when I insert 63, it shows a negative number, and when inserting 64 and beyond, it prints 0.
Edited by Icekilla - 10/31/12 at 6:46pm








