Program 31 - CSU1128 - CSE 2026 - Shoolini University

Program to print the triangle of numbers of the range entered by the user. Example 3 = 3 2 1 2 3

Explanation

The program prints a triangle 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: Stores d + 1 to 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 triangle pattern. The input is read using scanf("%d", &d);.

Triangle Pattern Printing

The program uses nested for loops to print the triangle pattern:

  • int q = d + 1;: Calculates q to be one greater than d.
  • for (m = 0; m <= d; m++): Outer loop to iterate over each row from 0 to d.
  • for (j = 1; j <= d - m; j++): Inner loop to print spaces before the numbers, ensuring the triangle shape.
  • for (o = 1; o <= m; o++): Inner loop to print the decreasing sequence of numbers from q - 1 to q - m.
  • for (int l = d - m; l <= d; l++): Inner loop to print the increasing sequence of numbers from d - m to d.
  • printf("\n");: Prints a new line after each row is complete.

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 triangle of numbers of 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 (Example: 4 = 4 3 2 1 2 3 4) - and press enter: ");
    scanf("%d", &d);
    printf("\n Triangle 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");
    }
    return 0;
}

                    
                

Output

Program to print the triangle of numbers of 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 (Example: 4 = 4 3 2 1 2 3 4) - and press enter: 5
Triangle 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