C program to triangle number pattern
- Home
- Floyd's triangle
- C program to triangle number pattern
- On
- By
- 0 Comment
- Categories: Floyd's triangle, Number pattern
C program to triangle number pattern
C program to triangle number pattern
In this tutorial, we will discuss how to write following C program to triangle number pattern
Here, some of Floyd’s triangle number patterns are written using nested for loop in C language
Program 1
Code to triangle number pattern 1
#include <stdio.h> #include <stdlib.h> int main() { int rows; printf("Triangle pattern 1\n\n"); printf("Enter the rows: "); scanf("%d",&rows); int i,j; for(i=1; i<=rows; i++) { for(j=i; j<=rows; j++) { printf("%d",i); } printf("\n"); } getch(); return 0; }
When the above code is executed, it produces the following results:
11111111 2222222 333333 44444 5555 666 77 8
Program 2
Code to triangle number pattern 2
#include <stdio.h> #include <stdlib.h> int main() { int rows; printf("Triangle pattern 1\n\n"); printf("Enter the rows: "); scanf("%d",&rows); int i,j; for(i=1; i<=rows; i++) { for(j=i; j<=rows; j++) { printf("%d",j); } printf("\n"); } getch(); return 0; }
When the above code is executed, it produces the following results:
12345678 2345678 345678 45678 5678 678 78 8
Program 3
Code to triangle number pattern 3
#include <stdio.h> #include <stdlib.h> int main() { int rows; printf("Triangle pattern 3\n\n"); printf("Enter the rows: "); scanf("%d",&rows); int i,j,k; for(i=rows; i>=1; i--) { if(i%2==1){k=1; } else {k=i;} for(j=1; j<=i; j++) { printf("%d",k); if(i%2==1){k++;} else {k--;} } printf("\n"); } getch(); return 0; }
When the above code is executed, it produces the following results:
87654321 1234567 654321 12345 4321 123 21 1
Program 4
Code to triangle number pattern 4
#include <stdio.h> #include <stdlib.h> int main() { int rows, i,j,k=1; printf("Enter the rows for pattern: "); scanf("%d",&rows); //input from user printf("here your pattern\n"); for(i=1; i<=rows; i++){ //for rows for(j=1; j<=i; j++){ printf("%d",k); k++; } k--; for(j=1; j<=i-1; j++){ k--; printf("%d",k); } printf("\n"); k=1; } getch(); return 0; }
When the above code is executed, it produces the following results:
Enter the rows for pattern:9 here your pattern 1 121 12321 1234321 123454321 12345654321 1234567654321 123456787654321 12345678987654321
Suggested for you
Do-while loop in Java language
Similar post
Java program to print star pyramid pattern
C program to print star pyramid pattern
C++ program to print star pyramid pattern
Python program to print star pyramid pattern
Floyd’s triangle number pattern using for loop in C
Floyd’s triangle pattern using nested for loop in Java
Floyd’s triangle pattern using nested while loop in Java