Pattern 24 : C Program to print triangle Number pattern using for loops
Program to print Triangle number pattern :
Let’s write a C Program to print the Triangle number pattern. Program will take one input number from the user and prints the pattern with that many number of rows.
Here is a Sample expected output, When user input value is 5
To print the given pattern, We are going to use two for loops. The Outer for loop and Inner for loop. The Outer for loop is used to print the rows and the inner for loop is used to print the columns.
So at each iteration of Outer for loop, The inner for loop will executes the given rows number of times.
The main logic is to stop printing the numbers when we reach the (j=i) Here 'i' outer loop iterator. 'j' inner loop iterator. So stopping when both i and j becomes equal, We are creating the diagonal shape of our Triangle pattern.
Triangle number pattern Program:
// Enter number of rows to print
printf("Enter how many rows you want : ");
// Iterate 'n' number of times
for(i=1; i<=n; i++)
// Continue only upto the 'i' value, So that we can form a Triangle
for(j=1; j<=i; j++)
// New line
Similar Number Patterns:
Similar Star pattern programs :
- Basic star pattern program (The square pattern with stars).
- Triangle star pattern program.
- Triangle star pattern program with Spaces.
- Pyramid star pattern.
- Reverse Pyramid program.
- Diamond Star pattern program.
- star pattern program.
- K-Star pattern program.
- Empty Rhombus program.
- X-Star pattern program