for(a=0;a<=b;a*a){
printf("%d\n",a);
a*a;
}
The first issue is in the for loop declaration, you just throw away the
a
variable freshly obtained from user, and set it to zero. Maybe you intended to use the
c
variable instead?
The second issue is that, when you write the expression
a * a
, you actually do not do anything
to the variable itself. You just put the value of
a
onto the stack, then do it a second time, then call the multiplication which pops the values from the stack and returns the result
to the stack. At this point, nothing has changed for
a
itself, which still holds its initial value. If you want it to remember the new value, you have to
assign the value which is on the stack to the variable, by using
a = a * a;
for example, or
a *= a;
in its shorter version.
The third issue is with the number of times your loop will be executed. With your implementation, if specified power is 6, you would execute the loop 7 times, for example (provided you fix the first issue).
- Either you start the counter at 0, and then you execute the loop until the counter is (power - 1),
- or you start at 1 and then you execute the loop until the counter is power.
Common standard in C language families is to use zero-based indices, so it's probably best to stick with it.
In clear:
for (counter = 0; counter < total; counter++) {
}
for (counter = 1; counter <= total; counter++) {
}
will both execute the loop the desired number of times (note all differences between both, especially the comparator in the test part: the first is a less-than operator, while the second is a less-than-or-equal one).
This smells like homework, so I will not write the code for you. I just hope these will guide you through your issues.