C While Statement Question


Recommended Posts

Ok I'm starting to learn C and I'm working with while statements. I'm trying to write a program that will let me enter an integer and then make a square out of asterisks with that integer I entered being the number of asterisks on a side.

Like if I enter 5 I should get this:

*****

*****

*****

*****

*****

Yea it's kind of a rectangle but lets call it a square lol.

This is what I have but it only prints one line of asterisks

#include <stdio.h>

   int main() {
   
       int side;
       int counter1 = 1;
       int counter2 = 1;
       
       printf("Enter an integer: ");
       scanf("%d", &side);
       while(counter1 <= side) {
               
               while(counter2 <= side) {
                   
                    printf("*");
                    counter2++;
                   
               }
               
               
               counter1++;
               printf("\n");
       }
       printf("Enter 1 to quit");
       scanf("%d", &side);
       return 0;
   }

Thanks in advance.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...