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:
And this puts out:Code:int i; int x=i * i; while (i<=12) {printf("%d ", x); i+=1; }
1 1 1 1 1 1 1 1 1 1 1 1 1
I know that I am close.
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:
And this puts out:Code:int i; int x=i * i; while (i<=12) {printf("%d ", x); i+=1; }
1 1 1 1 1 1 1 1 1 1 1 1 1
I know that I am close.
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.
I did that but I still got
1111111111111
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.
Well, did that and I am finally coming up with a different outcome, but it is still not it.
Here is my new code:
And that outcome is:Code:int i=1; for (i =1; i < 12; i +=i^2) {printf("%2d ", i); }
1 4 10
So I am getting closer.
Ok, why not look at the numbers again:
1 4 9 16 25 36
+3 +5 +7 +9 +11
See a pattern?
Yes, but how to I tell it to start at 3 then add two each time?
How about just putting x = i * i; in the loop in which i is incremented?
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
I already tried that and it didn't work.
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("%2d ", i); }
What part of "in the loop" is messing you up?
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
so do I make another for statement for do I put x = i*i in the original for statement?
Try:
Code:int i = 1, x; while (i<=12) { x = i*i; printf("%d ", x); i+=1; }
See I knew I was close.