#include <stdio.h>
#include <stdlib.h>
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
}
elseif (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;
}
}
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:
1 2 3 4 5 6 7 8 9 10
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;
...
}
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?
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.