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
For loop in C language
For loop in Python language
Nested for loop in Java language
Nested for loop in C++ language
Nested for loop in Python language
Floyd’s triangle number pattern using for loop in C
Floyd’s triangle pattern using nested for loop in Java
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…