C Program to Draw Rectangle Using for Loop

In this program, we will learn how to draw a rectangle using for loop in C Programming language.

This program asks the user to enter the number of rows and columns, then it will print the desired rectangle using for loop.

So, without further ado, let’s begin this tutorial.

C Program to Draw Rectangle Using for Loop

// C Program to Draw Rectangle Using for Loop
#include <stdio.h>
int main(){
    int rows, columns, i, j;
    
    // Asking for input
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    printf("Enter the number of columns: ");
    scanf("%d", &columns);
    
    for (i = 1; i < rows; i++){
        for (j = 1; j < columns; j++){
            printf("*");
        }
        printf("\n");
    }
    return 0;
}

Output

Enter the number of rows: 5
Enter the number of columns: 8
*******
*******
*******
*******

How Does This Program Work ?

    int rows, columns, i, j;

In this program, we have declared some int data type variables named rows, columns, i and j.

    // Asking for input
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    printf("Enter the number of columns: ");
    scanf("%d", &columns);

Then, the user is asked to enter the number of rows and columns.

    for (i = 1; i < rows; i++){
        for (j = 1; j < columns; j++){
            printf("*");
        }
        printf("\n");
    }

We have used a simple for loop logic which will print * in the form of a rectangle.

Conclusion

I hope after going through this post, you understand how to draw a rectangle using for loop in C Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to guide you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *