# Thread: Power Loop help!

1. ## Power Loop help!

I am trying to get this outcome:
1 4 9 16 25 36 49 64 81 100 121 144

Here is what I have so far:
Code:
```    int i;
int x=i * i;

while (i<=12)
{printf("%d ", x);
i+=1;
}```
And this puts out:
1 1 1 1 1 1 1 1 1 1 1 1 1

I know that I am close.

2. Well its printing all 1's because you are not settin the value of 'x' inside the loop. Try putting it in and see what happens. Its not goign to be right, nut it will be closer.

3. I did that but I still got
1111111111111

4. No you wouldent, or at least not if you set it as i*i each loop.

In your current prog 'i' is undefined when you set x as well.

5. Well, did that and I am finally coming up with a different outcome, but it is still not it.
Here is my new code:
Code:
```int i=1;

for (i =1; i < 12; i +=i^2)
{printf("&#37;2d ", i);
}```
And that outcome is:
1 4 10

So I am getting closer.

6. Ok, why not look at the numbers again:

1 4 9 16 25 36
+3 +5 +7 +9 +11

See a pattern?

7. Yes, but how to I tell it to start at 3 then add two each time?

8. How about just putting x = i * i; in the loop in which i is incremented?

9. I already tried that and it didn't work.

10. Originally Posted by hilow55321
I already tried that and it didn't work.
I don't see it in your code anywhere, and we keep suggesting it because it does work.

11. Here is the code that I am currently working on:
Code:
``` int i;
int x= i * i;
for (i =1; i <= 144; i +=x)
{printf("&#37;2d ", i);
}```

12. What part of "in the loop" is messing you up?

13. so do I make another for statement for do I put x = i*i in the original for statement?

14. Try:

Code:
```    int i = 1, x;

while (i<=12)
{
x = i*i;
printf("&#37;d ", x);
i+=1;
}```

15. See I knew I was close.

Popular pages Recent additions