Print a Simple Rectangle Using Asterisks in C Programming

C Programming: Print a Simple Rectangle Using Asterisks
When it comes to C programming, creating shapes with asterisks is a fundamental exercise that helps beginners understand loops, conditionals, and basic output techniques. In this article, we will guide you through the process of printing a simple rectangle using asterisks in C. We will provide a clear, step-by-step explanation of the code, the output you should expect, and additional insights to enhance your understanding of C programming.

Understanding the Basics of C Programming


Before diving into the specifics of printing a rectangle with asterisks, it’s crucial to grasp the fundamental concepts of C programming. C is a language used for system programming, application development, and embedded systems. It provides a robust set of features including loops, conditionals, and functions, which are essential for creating efficient and effective programs.

The Code for Printing a Simple Rectangle

C
//Visit: www.PickUpMySkills.com
//Programed by KAVIKUMARAN G

#include <stdio.h>

int main() {
    int rows, columns;
    
    
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    
    printf("Enter the number of columns: ");
    scanf("%d", &columns);
    
    for(int i = 1; i <= rows; i++) {
        for(int j = 1; j <= columns; j++) {
            printf("* ");
        }
        printf("\n"); 
    }
    
    return 0;
}

Explanation of Each Line:

C
include <stdio.h>
  • This line includes the standard input-output library (stdio.h), which allows the use of functions like printf (for output) and scanf (for input).
  • Without this, you wouldn’t be able to print or get user input in C.
C
int main() {
  • This is the main function thus the program always starts execution from the Main function.
  • Every C program starts with the main() function, and its return type is int, meaning it returns an integer value (usually 0 at the end of successful execution).
C
 int rows, columns;
  • two variables are decleared to store the number of rows and columns given by the user
  • the number of rows and columns by the user can be stored in the variable and it is used for looping
C
printf("Enter the number of rows: ");
  • The printf function is used to prompt the user to enter the number of rows.
  • the printf function will used to print message “Enter the number of rows: ” .
C
scanf("%d", &rows);
  • scanf is used to read the user input. It reads an integer input from the user and stores it in the rows variable.
  • %d is a format specifier for integers.
  • &rows is the memory address of the rows variable where the input will be stored.
C
printf("Enter the number of columns: ");
  • Another printf function is used to prompt the user to enter the number of columns.
  • the printf function print “Enter the number of columns: “
C
scanf("%d", &columns);
  • Like the previous scanf, this reads an integer input for the columns variable using the format specifier %d.
  • The input is stored at the memory address of columns (&columns).
C
for(int i = 1; i <= rows; i++) {
  • This is the outer loop which controls the number of rows of the rectangle.
  • The loop starts with i = 1 and runs as long as i is less than or equal to rows. It increments i by 1 on each iteration.
  • Each iteration represents a new row being printed.
C
for(int j = 1; j <= columns; j++) {
  • it the inner loop that is nested insde the outer loop and It controls the number of columns (asterisks) to be printed in each row.
  • The loop starts at j = 1 and runs until j <= columns. On each iteration, it prints an asterisk (*) followed by a space.
C
printf("* ");
  • This prints an asterisk (*) followed by a space in each column.
C
return 0;

Output:
C
*****************
*****************
*****************
*****************
*****************
*****************

Leave a Comment

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

Scroll to Top