Print Floyd's triangle

floyd triangle

Here given code implementation process.

// Java Program for
// Print Floyd's Triangle
class MyPattern
{
    // Print floyd triangle of given rows
    public void floydTriangle(int row)
    {
        if(row <= 0 )
        {
            return;
        }
        // Display number of row
        System.out.print("ROW SIZE : " + row + "\n\n");
      
        // Counter variable is used to display triangle values
        int counter = 1;
        // Outer loop for rows
        for (int i = 0; i < row; ++i)
        {
            for (int j = 0; j <= i; j++)
            {
                // Print element value
                System.out.print(" " + counter + "\t");
                // Increase counter value by one
                counter++;
            }
            // Include new line
            System.out.print("\n");
        }
        System.out.print("\n\n");
    }
    public static void main(String[] args)
    {
        MyPattern task = new MyPattern();
        // Test
        task.floydTriangle(7);
        task.floydTriangle(5);
    }
}

Output

ROW SIZE : 7

 1
 2   3
 4   5   6
 7   8   9   10
 11  12  13  14  15
 16  17  18  19  20  21
 22  23  24  25  26  27  28


ROW SIZE : 5

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

// Include header file
#include <iostream>
using namespace std;
// C++ Program for
// Print Floyd's Triangle
class MyPattern
{
    public:
        // Print floyd triangle of given rows
        void floydTriangle(int row)
        {
            if (row <= 0)
            {
                return;
            }
            // Display number of row
            cout << "ROW SIZE : " << row << "\n\n";
            // Counter variable is used to display triangle values
            int counter = 1;
            // Outer loop for rows
            for (int i = 0; i < row; ++i)
            {
                for (int j = 0; j <= i; j++)
                {
                    // Print element value
                    cout << " " << counter << "\t";
                    // Increase counter value by one
                    counter++;
                }
                // Include new line
                cout << "\n";
            }
            cout << "\n\n";
        }
};
int main()
{
    MyPattern *task = new MyPattern();
    // Test
    task->floydTriangle(7);
    task->floydTriangle(5);
    return 0;
}

Output

ROW SIZE : 7

 1
 2   3
 4   5   6
 7   8   9   10
 11  12  13  14  15
 16  17  18  19  20  21
 22  23  24  25  26  27  28


ROW SIZE : 5

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

// C Program
// Print Floyd's triangle
#include <stdio.h>

// Print floyd triangle of given rows
void floyd_triangle(int row)
{
    printf("ROW SIZE : %d\n\n", row);
    // declare loop control variable
    int i = 0;
    int j = 0;
    // counter variable is used to display triangle values
    int counter = 1;
    for (i = 0; i < row; ++i)
    {
        for (j = 0; j <= i; j++)
        {
            printf("%d\t", counter);
            counter++;
        }
        printf("\n");
    }
    printf("\n\n");
}
int main()
{
    //Test Case
    floyd_triangle(7);
    floyd_triangle(5);
    return 0;
}

Output

ROW SIZE : 7

1
2   3
4   5   6
7   8   9   10
11  12  13  14  15
16  17  18  19  20  21
22  23  24  25  26  27  28


ROW SIZE : 5

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

package main
import "fmt"
// Go Program for
// Print Floyd's Triangle

// Print floyd triangle of given rows
func floydTriangle(row int) {
    if row <= 0 {
        return
    }
    // Display number of row
    fmt.Print("ROW SIZE : ", row, "\n\n")
    // Counter variable is used to display triangle values
    var counter int = 1
    // Outer loop for rows
    for i := 0 ; i < row ; i++ {
        for j := 0 ; j <= i ; j++ {
            // Print element value
            fmt.Print(" ", counter, "\t")
            // Increase counter value by one
            counter++
        }
        // Include new line
        fmt.Print("\n")
    }
    fmt.Print("\n\n")
}
func main() {
    // Test
    floydTriangle(7)
    floydTriangle(5)
}

Output

ROW SIZE : 7

 1
 2   3
 4   5   6
 7   8   9   10
 11  12  13  14  15
 16  17  18  19  20  21
 22  23  24  25  26  27  28


ROW SIZE : 5

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

// Include namespace system
using System;
// Csharp Program for
// Print Floyd's Triangle
public class MyPattern
{
    // Print floyd triangle of given rows
    public void floydTriangle(int row)
    {
        if (row <= 0)
        {
            return;
        }
        // Display number of row
        Console.Write("ROW SIZE : " + row + "\n\n");
        // Counter variable is used to display triangle values
        int counter = 1;
        // Outer loop for rows
        for (int i = 0; i < row; ++i)
        {
            for (int j = 0; j <= i; j++)
            {
                // Print element value
                Console.Write(" " + counter + "\t");
                // Increase counter value by one
                counter++;
            }
            // Include new line
            Console.Write("\n");
        }
        Console.Write("\n\n");
    }
    public static void Main(String[] args)
    {
        MyPattern task = new MyPattern();
        // Test
        task.floydTriangle(7);
        task.floydTriangle(5);
    }
}

Output

ROW SIZE : 7

 1
 2   3
 4   5   6
 7   8   9   10
 11  12  13  14  15
 16  17  18  19  20  21
 22  23  24  25  26  27  28


ROW SIZE : 5

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

<?php
// Php Program for
// Print Floyd's Triangle
class MyPattern
{
    // Print floyd triangle of given rows
    public  function floydTriangle($row)
    {
        if ($row <= 0)
        {
            return;
        }
        // Display number of row
        print_r("ROW SIZE : ".strval($row).
            "\n\n");
        // Counter variable is used to display triangle values
        $counter = 1;
        // Outer loop for rows
        for ($i = 0; $i < $row; ++$i)
        {
            for ($j = 0; $j <= $i; $j++)
            {
                // Print element value
                print_r(" ".strval($counter).
                    "\t");
                // Increase counter value by one
                $counter++;
            }
            // Include new line
            print_r("\n");
        }
        print_r("\n\n");
    }
    public static
    function main($args)
    {
        $task = new MyPattern();
        // Test
        $task->floydTriangle(7);
        $task->floydTriangle(5);
    }
}
MyPattern::main(array());

Output

ROW SIZE : 7

 1
 2   3
 4   5   6
 7   8   9   10
 11  12  13  14  15
 16  17  18  19  20  21
 22  23  24  25  26  27  28


ROW SIZE : 5

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

// Node JS Program for
// Print Floyd's Triangle
class MyPattern
{
    // Print floyd triangle of given rows
    floydTriangle(row)
    {
        if (row <= 0)
        {
            return;
        }
        // Display number of row
        process.stdout.write("ROW SIZE : " + row + "\n\n");
        // Counter variable is used to display triangle values
        var counter = 1;
        // Outer loop for rows
        for (var i = 0; i < row; ++i)
        {
            for (var j = 0; j <= i; j++)
            {
                // Print element value
                process.stdout.write(" " + counter + "\t");
                // Increase counter value by one
                counter++;
            }
            // Include new line
            process.stdout.write("\n");
        }
        process.stdout.write("\n\n");
    }
}

function main()
{
    var task = new MyPattern();
    // Test
    task.floydTriangle(7);
    task.floydTriangle(5);
}
main();

Output

ROW SIZE : 7

 1
 2   3
 4   5   6
 7   8   9   10
 11  12  13  14  15
 16  17  18  19  20  21
 22  23  24  25  26  27  28


ROW SIZE : 5

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

#  Python 3 Program for
#  Print Floyd's Triangle
class MyPattern :
    #  Print floyd triangle of given rows
    def floydTriangle(self, row) :
        if (row <= 0) :
            return
        
        #  Display number of row
        print("ROW SIZE : ", row ,"\n")
        #  Counter variable is used to display triangle values
        counter = 1
        i = 0
        #  Outer loop for rows
        while (i < row) :
            j = 0
            while (j <= i) :
                #  Print element value
                print(" ", counter ,"\t", end = "")
                #  Increase counter value by one
                counter += 1
                j += 1
            
            #  Include new line
            print(end = "\n")
            i += 1
        
        print("\n")
    

def main() :
    task = MyPattern()
    #  Test
    task.floydTriangle(7)
    task.floydTriangle(5)

if __name__ == "__main__": main()

Output

ROW SIZE :  7

  1
  2       3
  4       5       6
  7       8       9       10
  11      12      13      14      15
  16      17      18      19      20      21
  22      23      24      25      26      27      28


ROW SIZE :  5

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

#  Ruby Program for
#  Print Floyd's Triangle
class MyPattern 
    #  Print floyd triangle of given rows
    def floydTriangle(row) 
        if (row <= 0) 
            return
        end

        #  Display number of row
        print("ROW SIZE : ", row ,"\n\n")
        #  Counter variable is used to display triangle values
        counter = 1
        i = 0
        #  Outer loop for rows
        while (i < row) 
            j = 0
            while (j <= i) 
                #  Print element value
                print(" ", counter ,"\t")
                #  Increase counter value by one
                counter += 1
                j += 1
            end

            #  Include new line
            print("\n")
            i += 1
        end

        print("\n\n")
    end

end

def main() 
    task = MyPattern.new()
    #  Test
    task.floydTriangle(7)
    task.floydTriangle(5)
end

main()

Output

ROW SIZE : 7

 1  
 2   3  
 4   5   6  
 7   8   9   10 
 11  12  13  14  15 
 16  17  18  19  20  21 
 22  23  24  25  26  27  28 


ROW SIZE : 5

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


// Scala Program for
// Print Floyd's Triangle
class MyPattern()
{
    // Print floyd triangle of given rows
    def floydTriangle(row: Int): Unit = {
        if (row <= 0)
        {
            return;
        }
        // Display number of row
        print("ROW SIZE : " + row + "\n\n");
        // Counter variable is used to display triangle values
        var counter: Int = 1;
        var i: Int = 0;
        // Outer loop for rows
        while (i < row)
        {
            var j: Int = 0;
            while (j <= i)
            {
                // Print element value
                print(" " + counter + "\t");
                // Increase counter value by one
                counter += 1;
                j += 1;
            }
            // Include new line
            print("\n");
            i += 1;
        }
        print("\n\n");
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var task: MyPattern = new MyPattern();
        // Test
        task.floydTriangle(7);
        task.floydTriangle(5);
    }
}

Output

ROW SIZE : 7

 1
 2   3
 4   5   6
 7   8   9   10
 11  12  13  14  15
 16  17  18  19  20  21
 22  23  24  25  26  27  28


ROW SIZE : 5

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

// Swift 4 Program for
// Print Floyd's Triangle
class MyPattern
{
    // Print floyd triangle of given rows
    func floydTriangle(_ row: Int)
    {
        if (row <= 0)
        {
            return;
        }
        // Display number of row
        print("ROW SIZE : ", row ,"\n");
        // Counter variable is used to display triangle values
        var counter: Int = 1;
        var i: Int = 0;
        // Outer loop for rows
        while (i < row)
        {
            var j: Int = 0;
            while (j <= i)
            {
                // Print element value
                print(" ", counter ,"\t", terminator: "");
                // Increase counter value by one
                counter += 1;
                j += 1;
            }
            // Include new line
            print(terminator: "\n");
            i += 1;
        }
        print("\n");
    }
}
func main()
{
    let task: MyPattern = MyPattern();
    // Test
    task.floydTriangle(7);
    task.floydTriangle(5);
}
main();

Output

ROW SIZE :  7

  1
  2       3
  4       5       6
  7       8       9       10
  11      12      13      14      15
  16      17      18      19      20      21
  22      23      24      25      26      27      28


ROW SIZE :  5

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

// Kotlin Program for
// Print Floyd's Triangle
class MyPattern
{
    // Print floyd triangle of given rows
    fun floydTriangle(row: Int): Unit
    {
        if (row <= 0)
        {
            return;
        }
        // Display number of row
        print("ROW SIZE : " + row + "\n\n");
        // Counter variable is used to display triangle values
        var counter: Int = 1;
        var i: Int = 0;
        // Outer loop for rows
        while (i < row)
        {
            var j: Int = 0;
            while (j <= i)
            {
                // Print element value
                print(" " + counter + "\t");
                // Increase counter value by one
                counter += 1;
                j += 1;
            }
            // Include new line
            print("\n");
            i += 1;
        }
        print("\n\n");
    }
}
fun main(args: Array < String > ): Unit
{
    val task: MyPattern = MyPattern();
    // Test
    task.floydTriangle(7);
    task.floydTriangle(5);
}

Output

ROW SIZE : 7

 1
 2   3
 4   5   6
 7   8   9   10
 11  12  13  14  15
 16  17  18  19  20  21
 22  23  24  25  26  27  28


ROW SIZE : 5

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

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved