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
Example:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include<stdio.h> int main() { int i,j,k,n; // Enter number of rows to print printf("Enter how many rows you want : "); scanf("%d",&n); // 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++) printf(" %d",j); // New line printf("\n"); } return 0; } |
Program Output:
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