C Program to print Floyd's triangle - GeeksforGeeks

24 Feb, 2022
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 program prints Floyd’s triangle with n lines.  
C++
C
Java
Python3
C#
PHP
Javascript
#include <bits/stdc++.h>using namespace std; void printFloydTriangle(int n){    int i, j, val = 1;    for (i = 1; i <= n; i++)    {        for (j = 1; j <= i; j++)            cout << val++ << " ";        cout << endl;    }} // Driver Codeint main(){    printFloydTriangle(6);    return 0;} // This is code is contributed// by rathbhupendra
// Without using a temporary variable and with only one loop#include<stdio.h>void floyd(n){    int i,j=1;    for (i=1;i<=(n*(n+1))/2;i++){        printf("%d ",i);        if(i==(j*(j+1))/2){            printf("\n");            j++;        }    }} int main(){    floyd(6);} //This code is contributed by Vishal B
// Java program to print// Floyd's triangleclass GFG{    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 Code    public static void main(String[] args)    {        printFloydTriangle(6);    }}
# Python3 program to print# Floyd's triangledef floydTriangle(n):     val = 1    for i in range(1, n + 1):         for j in range(1, i + 1):            print(val, end = " ")            val += 1                 print("") floydTriangle(6) # This code is contributed by# Smitha Dinesh Semwal
// C# program to print// Floyd's triangleusing System; class GFG{    static void printFloydTriangle(int n)    {        int i, j, val = 1;        for (i = 1; i <= n; i++)        {            for (j = 1; j <= i; j++)            {                Console.Write(val + " ");                val++;            }            Console.WriteLine();        }    }             // Driver Code    public static void Main()    {        printFloydTriangle(6);    }}
<?php// PHP code to print Floyd's Triangle // Function to display Floyd's Trianglefunction FloydsTriangle($n){    $val = 1;         // loop for number of lines    for($i = 1; $i <= $n; $i++)    {        // loop for number of elements        // in each line        for($j = 1; $j <= $i; $j++)        {            print($val." ");            $val++;        }        print("\n");    }} // Driver's Code$n = 6;FloydsTriangle($n); // This code is contributed by akash7981?>
<script>// Javascript implementationfunction printFloydTriangle(n){    var i, j, val = 1;    for (i = 1; i <= n; i++)    {        for (j = 1; j <= i; j++)            document.write(val++ + " ");        document.write("<br>");    }} // Driver CodeprintFloydTriangle(6); // This is code is contributed// by shivani</script>
Output:  
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
Time Complexity: O(n2)
Auxiliary Space: O(1) 
https://www.youtube.com/watch?v=VOk9
-jsvpRc 
 
 
rathbhupendra
VishalB1
shivanisinghss2110
subhammahato348
simmytarika5
pattern-printing
C Language
Mathematical
School Programming
Mathematical
pattern-printing
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Function Pointer in C
fork() in C
std::string class in C++
Enumeration (or enum) in C
TCP Server-Client implementation in C
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7