C Program to print Floyd’s triangle

1

Floyd’s triangle is a triangle with first natural numbers.

1
2     3
4     5     6
7     8     9     10
11     12     13     14     15

Following C program prints Floyd’s triangle with n lines.

C

#include <stdio.h>

void printFloydTriangle(int n)
{
   int i, j, val = 1;
   for (i=1; i<=n; i++)
   {
       for (j=1; j<=i; j++)
         printf("%d ", val++);
       printf("\n");
   }
}

int main()
{
    printFloydTriangle(6);
    return 0;
}

Java

// Java program to print
// Floyd's triangle
    
class Test
{
    static void printFloydTriangle(int n)
    {
       int i, j, val = 1;
       for (i=1; i<=n; i++)
       {
           for (j=1; j<=i; j++){
             System.out.print(val+" ");
             val++;
           }
           System.out.println();
             
       }
    }
         
    // Driver method
    public static void main(String[] args) 
    {
        printFloydTriangle(6);
    }
}


Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



1 Average Difficulty : 1/5.0
Based on 4 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.