Explanation
The program generates a rhombus pattern using the number entered by the user through function recursion. The rhombus is formed by printing a symmetrical pattern of numbers.
Header
The code begins with including the standard input-output header file using #include <stdio.h>
.
Function Declaration
The program declares a recursive function print_rhombus
to print a row of the rhombus:
void print_rhombus(int d, int m);
: Declares the function with two integer parametersd
andm
.
Variable Declaration
The main()
function is defined, and a variable is declared:
int d
: Variable to hold the number entered by the user.
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:
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: ");
: Prompts the user to enter a number.scanf("%d", &d);
: Reads the number and stores it ind
.
Print Rhombus
The program prints the rhombus pattern:
print_rhombus(d, 0);
: Calls the recursive function to print the upper half of the rhombus.for (int m = 1; m <= d; m++)
: Loops through each row to print the lower half of the rhombus.for (int j = 1; j <= m; j++)
: Prints the leading spaces for the current row.for (int l = d; l >= m; l--)
: Prints the decreasing sequence of numbers in the current row.for (int o = 1; o <= d - m; o++)
: Prints the increasing sequence of numbers in the current row.
Function Definition
The program defines the print_rhombus
function:
void print_rhombus(int d, int m)
: Defines the function with two integer parametersd
andm
.for (int j = 1; j <= d - m; j++)
: Prints the leading spaces for the current row.for (int o = 1; o <= m; o++)
: Prints the decreasing sequence of numbers in the current row.for (int l = d - m; l <= d; l++)
: Prints the increasing sequence of numbers in the current row.printf("\n");
: Prints a newline after each row.if (m < d)
: Calls the function recursively to print the next row.print_rhombus(d, m + 1);
: Recursive call to print the next row.
Program End
The program ends with return 0;
to indicate successful execution.
Code
/*
* -----------------------------------------------------------
* Logic Building with Computer Programming (CSU1128)
* Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
*
* 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>
void print_rhombus();
int main()
{
printf("\n\n Program to generate rhombus (Program 32) by using the number entered by the user through function recursion. \n\n");
// Read the number from the user
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: ");
int d;
scanf("%d", &d);
printf("\n Rhombus for the entered range %d is: \n", d);
// Print the upper half of the rhombus
print_rhombus(d, 0);
// Print the lower half of the rhombus
for (int m = 1; m <= d; m++)
{
// Print the leading spaces for the current row
for (int j = 1; j <= m; j++)
{
printf(" ");
}
// Print the numbers in the current row
for (int l = d; l >= m; l--)
{
printf(" %d", l);
}
for (int o = 1; o <= d - m; o++)
{
printf(" %d", o + m);
}
printf("\n");
}
return 0;
}
// Declare a recursive function to print a row of the rhombus
void print_rhombus(int d, int m)
{
// Print the leading spaces for the current row
for (int j = 1; j <= d - m; j++)
{
printf(" ");
}
// Print the numbers in the current row
for (int o = 1; o <= m; o++)
{
printf(" %d", d + 1 - o);
}
for (int l = d - m; l <= d; l++)
{
printf(" %d", l);
}
printf("\n");
// Call the function recursively to print the next row
if (m < d)
{
print_rhombus(d, m + 1);
}
}
Output
Program to generate rhombus (Program 32) by using the number entered by the user through function recursion.
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: 4
Rhombus for the entered range 4 is:
4
4 3 4
4 3 2 3 4
4 3 2 1 2 3 4
4 3 2 1 0 1 2 3 4
4 3 2 1 2 3 4
4 3 2 3 4
4 3 4
4