Floyd's triangle

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

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

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

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

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

For loop in C++ language

For loop in Java language

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 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

Floyd’s triangle pattern using nested while loop in Java

C program to pyramid number pattern
C program to Inverted pyramid number pattern
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago