I am trying to generate a table of squares using for, while, and do loops. The loop will square every number between and including 1 and the number the user puts in. At the end of the for, while, and do loops, the user will be asked if they wish to do another table. Entering 'y' will prompt for another integer and so on.
Alright, so here's my code:
Code:
#include <stdio.h>
main (void)
{
int x, j;
char choice;
printf("Please enter a number (1-1000) to display the squares:");
scanf("%d", &x);
x = getchar ();
j = 1;
while ( j <= x)
{
j++;
printf("Squares Table: %d %d \n", j, j*j);
}
/*{ for (j <=x;);
j++;
printf("Squares Table: %d %d \n", j, j*j);
}
*/
do
{
j++;
printf("Squares Table: %d %d \n", j, j*j);
}
while (j <=x);
printf("Would you like to generate a squares table (y/n)?");
scanf("%c", &choice);
choice = getchar(); /*Assign to character*/
while (choice == 'y');
}
I'm having a problem with the 'y' restarting the loop (instead of just exiting). Also, no matter what number I enter, the loop always stops at squaring 12.
And the for loop is just confusing the heck out of me.
I'm sorry if this sounds like a lot - I'm very new at this and it is not treating me right. Thanks so much for your help.