Help needed to print the value nth number raised to the power n.
-
Hello People, I am quite a newbie to C++ programming and I got stuck up with a simple program. The program requires me to print the value for 2 raised to the power 20. I actually had the idea creeping about this program since 1 megabyte is 2 raised to the power 20. I wanted to find the result of that via this piece of code:
#include
#include
int main()
{
int a=2;
for (int i=2; i<=20; i++)
{
a=a*a;
}
cout<and the output I get is: 0
The compiler/IDE I'm using is Borland C++ (latest version) but I know theres nothing wrong with the compiler though. There must be some logical mistake. Rectification would be appreciated folks. Hoping for help.
Thanks!
Rajdeep_ -
Hello People, I am quite a newbie to C++ programming and I got stuck up with a simple program. The program requires me to print the value for 2 raised to the power 20. I actually had the idea creeping about this program since 1 megabyte is 2 raised to the power 20. I wanted to find the result of that via this piece of code:
#include
#include
int main()
{
int a=2;
for (int i=2; i<=20; i++)
{
a=a*a;
}
cout<and the output I get is: 0
The compiler/IDE I'm using is Borland C++ (latest version) but I know theres nothing wrong with the compiler though. There must be some logical mistake. Rectification would be appreciated folks. Hoping for help.
Thanks!
Rajdeep_ -
You are not raising it to the 20th power, you are repeatedly squaring the value, The first time through the loop you get 4 (2*2), then 16 (4*4), then 256 (16*16), and so on.
Use the best guess
-
Hello People, I am quite a newbie to C++ programming and I got stuck up with a simple program. The program requires me to print the value for 2 raised to the power 20. I actually had the idea creeping about this program since 1 megabyte is 2 raised to the power 20. I wanted to find the result of that via this piece of code:
#include
#include
int main()
{
int a=2;
for (int i=2; i<=20; i++)
{
a=a*a;
}
cout<and the output I get is: 0
The compiler/IDE I'm using is Borland C++ (latest version) but I know theres nothing wrong with the compiler though. There must be some logical mistake. Rectification would be appreciated folks. Hoping for help.
Thanks!
Rajdeep_a is a integer data type :) .you are printing the value by multiplying it 19 times and which in return it exceeds more than LONG data type :sigh: .hence your output is always zero :doh: . There is no compiler problem. :) I think you got what I am explaining you. :omg:
-
Hello People, I am quite a newbie to C++ programming and I got stuck up with a simple program. The program requires me to print the value for 2 raised to the power 20. I actually had the idea creeping about this program since 1 megabyte is 2 raised to the power 20. I wanted to find the result of that via this piece of code:
#include
#include
int main()
{
int a=2;
for (int i=2; i<=20; i++)
{
a=a*a;
}
cout<and the output I get is: 0
The compiler/IDE I'm using is Borland C++ (latest version) but I know theres nothing wrong with the compiler though. There must be some logical mistake. Rectification would be appreciated folks. Hoping for help.
Thanks!
Rajdeep_ -
Hello People, I am quite a newbie to C++ programming and I got stuck up with a simple program. The program requires me to print the value for 2 raised to the power 20. I actually had the idea creeping about this program since 1 megabyte is 2 raised to the power 20. I wanted to find the result of that via this piece of code:
#include
#include
int main()
{
int a=2;
for (int i=2; i<=20; i++)
{
a=a*a;
}
cout<and the output I get is: 0
The compiler/IDE I'm using is Borland C++ (latest version) but I know theres nothing wrong with the compiler though. There must be some logical mistake. Rectification would be appreciated folks. Hoping for help.
Thanks!
Rajdeep_You need to multiply
a
by2
twenty times. Something like:int a = 1;
for (int i = 0; i < 20; i++)
a = a * 2;"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous