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
//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:
include <stdio.h>
- This line includes the standard input-output library (
stdio.h
), which allows the use of functions likeprintf
(for output) andscanf
(for input). - Without this, you wouldn’t be able to print or get user input in 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 isint
, meaning it returns an integer value (usually 0 at the end of successful execution).
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
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: ” .
scanf("%d", &rows);
scanf
is used to read the user input. It reads an integer input from the user and stores it in therows
variable.%d
is a format specifier for integers.&rows
is the memory address of therows
variable where the input will be stored.
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: “
scanf("%d", &columns);
- Like the previous
scanf
, this reads an integer input for thecolumns
variable using the format specifier%d
. - The input is stored at the memory address of
columns
(&columns
).
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 asi
is less than or equal torows
. It incrementsi
by 1 on each iteration. - Each iteration represents a new row being printed.
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.
printf("* ");
- This prints an asterisk (
*
) followed by a space in each column.
return 0;
Output:
*****************
*****************
*****************
*****************
*****************
*****************