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
#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
#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
#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
#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
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…