Hey guys, I was working on a program to display an output
Code:
1
2 4
3 9 27
4 16 64 256
I got my code working but I noticed something funny...
Here's what the code with var b defined in the inner 'for' loop
Code:
#include <stdio.h>
#include<math.h>
void main(void) {
double a=1,b;
for(;a<5;a++)
{
for(b=1;b<=a;b++)
{
//printf("b is %d\tand a is %d\n",b,a);
printf("%.0lf\t",pow(a,b));
}
printf("\n");
}
}
The output is as desired in this case...
But when I assign the value of b to the declaration statement like this...
Code:
#include <stdio.h>
#include<math.h>
void main(void) {
double a=1,b=1;
for(;a<5;a++)
{
for(;b<=a;b++)
{
//printf("b is %d\tand a is %d\n",b,a);
printf("%.0lf\t",pow(a,b));
}
printf("\n");
}
}
I get a wrong output like this (?!)
Oh, and the commented line shows that var b is always zero EVEN though it is incremented in the loop...
Here's the output with that line uncommented and the subsequent line commented...
Code:
b is 0 and a is 1072693248
b is 0 and a is 1073741824
b is 0 and a is 1074266112
b is 0 and a is 1074790400
Thanks in advance for all the help, guys!
Have a great day!