Программа C для печати треугольника Флойда

Опубликовано: 21 Января, 2022

Треугольник Флойда - это треугольник с первыми натуральными числами.

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

#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 Code
int main()
{
    printFloydTriangle(6);
    return 0;
}
 
// This is code is contributed
// by rathbhupendra

C

// 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(" ");
            j++;
        }
    }
}
 
int main(){
    floyd(6);
}
 
//This code is contributed by Vishal B

Java

// Java program to print
// Floyd"s triangle
class 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

# Python3 program to print
# Floyd"s triangle
def loydTriangle(n):
 
    val = 1
    for i in range(1, n + 1):
 
        for j in range(1, i + 1):
            print(val, end = " ")
            val += 1
         
        print("")
 
loydTriangle(6)
 
# This code is contributed by
# Smitha Dinesh Semwal

C#

// C# program to print
// Floyd"s triangle
using 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
// PHP code to print Floyd"s Triangle
 
// Function to display Floyd"s Triangle
function 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(" ");
    }
}
 
// Driver"s Code
$n = 6;
FloydsTriangle($n);
 
// This code is contributed by akash7981
?>

Javascript

<script>
// Javascript implementation
function 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 Code
printFloydTriangle(6);
 
// This is code is contributed
// by shivani
</script>

Выход:

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

https://www.youtube.com/watch?v=VOk9

-jsvpRc

Вниманию читателя! Не прекращайте учиться сейчас. Освойте все важные концепции DSA с помощью самостоятельного курса DSA по приемлемой для студентов цене и будьте готовы к работе в отрасли. Чтобы завершить подготовку от изучения языка к DS Algo и многому другому, см. Полный курс подготовки к собеседованию .

Если вы хотите посещать живые занятия с отраслевыми экспертами, пожалуйста, обращайтесь к Geeks Classes Live и Geeks Classes Live USA.