### Assist with while looping

closed account (oy721hU5)
Please lend me a helping hand. I have printed six different series of numbers. The requirement was to only use one and only one single while loop.

 ``1234567891011121314151617181920212223242526272829303132`` `````` #include #include int main() { int k; k = 0; while (k < 10) { if (k < 9) { printf("%5d,", k+1 ); // these numbers should be a line by itself printf("%5d,", 2*k+2 ); // these numbers should be a line by itself printf("%5d,", 3*k+3 ); // these numbers should be a line by itself printf("%5d,", 10*k+10 ); // these numbers should be a line by itself printf("%5d,", 2*k+1 ); // these numbers should be a line by itself printf("%5d,", (k+1)*(k+1) ); // these numbers should be a line by itself } else if (k = 9) { printf("%5d", k+1 ); // these will simply print the last number of the loop, each correspond to one line printf("%5d", 2*k+2 ); // these will simply print the last number of the loop, each correspond to one line printf("%5d", 3*k+3 ); // these will simply print the last number of the loop, each correspond to one line printf("%5d", 10*k+10 ); // these will simply print the last number of the loop, each correspond to one line printf("%5d", 2*k+1 ); // these will simply print the last number of the loop, each correspond to one line printf("%5d", (k+1)*(k+1) ); // these will simply print the last number of the loop, each correspond to one line } else{} k = k+1; } }``````

The output must look similar to this:
 ``` 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 3, 6, 9, 12, 15, 18, 21, 24, 27, 30 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 ```

if you want to print out 6 lines you need to cycle 6 times more:

`while (k < (10 * 6))`

use a switch to separate the lines:
 ``12345678910`` ``````switch(k / 10) { case 0: printf("%5d", k+1 ); // these numbers should be a line by itself break; case 1: printf("%5d", 2*k+2 ); // these numbers should be a line by itself break; ... }``````

to print the comma and the end of line do this:
 ``1234`` ``````if(k % 10) printf(", "); else if(k > 0) printf("\n");``````
Was just reviewing over the some of these questions on the forum. Just a beginner, what does "%5d" do? I thought it was suppose to print out a message on the console. Also is printf same as cout?
@fahmankhan75,

Have a look in the reference section at the top left of this page.
line 19 has an error.

should use two equal signs.

You are using only 'c' code here.

I am not good with c style code but here is the solution with c++

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758`` ``````#include using namespace std; int main() { int a=1, b=0, c=0; while(true) { if(c==0) { cout << a+b << ", "; b++; } if(b==10 && c==0) { cout << endl; c++; b=2; } if(c==1) { cout << a*b << ", "; b=b+2; } if(b==22 && c==1) { cout << endl; c++; b=3; } if(c==2) { cout << a*b << ", "; b=b+3; } if(b==33 && c==2) { cout << endl; c++; b=10; } if(c==3) { cout << a*b << ", "; b=b+10; } if(b==110 && c==3) { cout << endl; c++; b=1; } if(c==4) { cout << a << ", "; b=b+2; a=a+b; } if(a>100 && c==4) { cout << endl; break; } } cin.get(); return 0; }``````

the secret to using only one while loop is to work with 3 variables.
the first is your starting value, the second is to change the first with logical pattern and the third controls flow in the loop.
Last edited on
Change line 19:
`else if (k = 9)`
to:
`else if (k == 9)`
Topic archived. No new replies allowed.