Program 30 - CSU1128 - CSE 2026 - Shoolini University

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

Explanation

The program prints a triangle pattern of numbers for a range entered by the user. The pattern includes numbers in increasing order followed by numbers in decreasing 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 n are declared:

  • int d: Used as an outer loop counter for rows.
  • int m: Used as a loop counter for spaces.
  • int j: Used as a loop counter for the increasing sequence of numbers.
  • int o: Used as a loop counter for the decreasing sequence of numbers.
  • int n: Stores the range 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 for which they want to print its triangle pattern. The input is read using scanf("%d", &n);.

Triangle Pattern Printing

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

  • for (d = 1; d <= n; d++): Outer loop to iterate over each row from 1 to n.
  • for (m = 1; m <= n - d; m++): Inner loop to print spaces before the numbers, ensuring the triangle shape.
  • for (j = 1; j <= d; j++): Inner loop to print the increasing sequence of numbers from 1 to d.
  • for (o = d - 1; o >= 1; o--): Inner loop to print the decreasing sequence of numbers from d - 1 to 1.
  • 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: 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>
int main()
{
    int d, m, j, o, n;
    printf("\n\n Program to print the triangle of numbers of the range entered by the user. Example 3 = 1 2 3 2 1 \n\n");

    printf("Enter a number for which you want to print its triangle and its reverse (Example: 4 = 1 2 3 4 3 2 1) - and press enter: ");
    scanf("%d", &n);
    printf("\n Triangle for the entered range is %d is: \n", n);
    for (d = 1; d <= n; d++)
    {
        for (m = 1; m <= n - d; m++)
        {
            printf("  ");
        }
        for (j = 1; j <= d; j++)
        {
            printf("%d ", j);
        }
        for (o = d - 1; o >= 1; o--)
        {
            printf("%d ", o);
        }
        printf("\n");
    }
    return 0;
}
                    
                

Output

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

Enter a number for which you want to print its triangle and its reverse (Example: 4 = 1 2 3 4 3 2 1) - and press enter: 5
Triangle for the entered range is 5 is:

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1