Explanation
The program prints a rhombus pattern of numbers for a range entered by the user. The pattern includes numbers in decreasing order followed by numbers in increasing order, forming a symmetrical shape.
Header
The code begins with including the standard input-output header file using #include <stdio.h>.
Variable Declaration
The main() function is defined, and five integer variables d, m, j, o, and q are declared:
int d: Stores the range entered by the user.int m: Used as an outer loop counter for rows.int j: Used as a loop counter for spaces.int o: Used as a loop counter for the decreasing sequence of numbers.int q: Storesd + 1to help with the decreasing sequence calculation.
Program Description
The program prints a description message for the user to understand its functionality.
User Input
The user is prompted to enter a number for which they want to print its rhombus pattern. The input is read using scanf("%d", &d);.
Rhombus Pattern Printing
The program uses nested for loops to print the rhombus pattern:
int q = d + 1;: Calculatesqto be one greater thand.for (m = 0; m <= d; m++): Outer loop to iterate over each row from 0 tod(top half of the rhombus).for (j = 1; j <= d - m; j++): Inner loop to print spaces before the numbers, ensuring the rhombus shape.for (o = 1; o <= m; o++): Inner loop to print the decreasing sequence of numbers fromq - 1toq - m.for (int l = d - m; l <= d; l++): Inner loop to print the increasing sequence of numbers fromd - mtod.printf("\n");: Prints a new line after each row is complete.for (m = 0; m <= d; m++): Outer loop to iterate over each row from 0 tod(bottom half of the rhombus).for (j = 1; j <= m; j++): Inner loop to print spaces before the numbers, ensuring the rhombus shape.for (int l = d; l >= m; l--): Inner loop to print the decreasing sequence of numbers fromdtom.for (o = 1; o <= d - m; o++): Inner loop to print the increasing sequence of numbers fromm + 1tod.
Program End
The program ends with return 0; to indicate successful execution.
Code
/*
* -----------------------------------------------------------
* Logic Building with Computer Programming (CSU1128)
* Instructor: Dr. Pankaj Vaidya | Author: Vanshika Painuly
*
* This code is a part of the educational initiative by dmj.one
* with aim of empowering and inspiring learners in the field of
* Computer Science and Engineering through the respective courses.
*
* (c) 2022, Divya Mohan for dmj.one. All rights reserved.
* -----------------------------------------------------------
*/
#include <stdio.h>
int main()
{
int d, m, j, o = 0, n;
printf("\n\n Program to print the rhombus of numbers from the range entered by the user. Example 3 = 3 2 1 2 3 \n\n");
printf("Enter a number for which you want to print its triangle and its reverse to form a rhombus (Example: 4 = 4 3 2 1 2 3 4) - and press enter: ");
scanf("%d", &d);
printf("\n Rhombus for the entered range %d is: \n", d);
int q = d + 1;
for (m = 0; m <= d; m++)
{
for (j = 1; j <= d - m; j++)
{
printf(" ");
}
for (o = 1; o <= m; o++)
{
printf(" %d", q - o);
}
for (int l = d - m; l <= d; l++)
{
printf(" %d", l);
}
printf("\n");
}
for (m = 0; m <= d; m++)
{
for (j = 1; j <= m; j++)
{
printf(" ");
}
for (int l = d ; l >= m; l--)
{
printf(" %d", l);
}
for (o = 1; o <= d-m;o++)
{
printf(" %d", o+m);
}
printf("\n");
}
return 0;
}
Output
Program to print the rhombus of numbers frpm the range entered by the user. Example 3 = 3 2 1 2 3
Enter a number for which you want to print its triangle and its reverse to form a rhombus (Example:
4 = 4 3 2 1 2 3 4)
- and press enter: 5
Rhombus for the entered range 5 is:
5
5 4 5
5 4 3 4 5
5 4 3 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 1 0 1 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 3 4 5
5 4 3 4 5
5 4 5
5