Pascal's triangle patterns

Pascal triangle

Here given code implementation process.

/*
  Java Program
  Pascal's triangle patterns
*/
public class PascalPattern
{
    // Print the pascal triangle of given size
    public void pascalTriangle(int size)
    {
        // Variables that are the control executors of this function
        int i = 0, j = 0, k = 0;
        for (i = 0; i < size; i++)
        {
            // Print initial white space
            for (j = i + 1; j < size; j++)
            {
                System.out.print("  ");
            }
            for (j = 0; j <= i; j++)
            {
                if (j == 0 || i == 0)
                {
                    // Set initial values
                    k = 1;
                }
                else
                {
                    // Pascal's triangle number calculation
                    k = k *(i - j + 1) / j;
                }
                // Display the value of calculate number
                System.out.print(" " + k + "  ");
            }
            System.out.print("\n");
        }
        System.out.print("\n");
    }
    public static void main(String[] args)
    {
        PascalPattern task = new PascalPattern();
        // Test Case
        task.pascalTriangle(5);
        task.pascalTriangle(7);
    }
}

Output

         1
       1   1
     1   2   1
   1   3   3   1
 1   4   6   4   1

             1
           1   1
         1   2   1
       1   3   3   1
     1   4   6   4   1
   1   5   10   10   5   1
 1   6   15   20   15   6   1
// Include header file
#include <iostream>
using namespace std;
/*
  C++ Program
  Pascal's triangle patterns
*/
class PascalPattern
{
    public:
        // Print the pascal triangle of given size
        void pascalTriangle(int size)
        {
            // Variables that are the control executors of this function
            int i = 0;
            int j = 0;
            int k = 0;
            for (i = 0; i < size; i++)
            {
                // Print initial white space
                for (j = i + 1; j < size; j++)
                {
                    cout << "  ";
                }
                for (j = 0; j <= i; j++)
                {
                    if (j == 0 || i == 0)
                    {
                        // Set initial values
                        k = 1;
                    }
                    else
                    {
                        // Pascal's triangle number calculation
                        k = k *(i - j + 1) / j;
                    }
                    // Display the value of calculate number
                    cout << " " << k << "  ";
                }
                cout << "\n";
            }
            cout << "\n";
        }
};
int main()
{
    PascalPattern *task = new PascalPattern();
    // Test Case
    task->pascalTriangle(5);
    task->pascalTriangle(7);
    return 0;
}

Output

         1
       1   1
     1   2   1
   1   3   3   1
 1   4   6   4   1

             1
           1   1
         1   2   1
       1   3   3   1
     1   4   6   4   1
   1   5   10   10   5   1
 1   6   15   20   15   6   1
/*
  C Program
  Pascal's triangle patterns
*/
#include <stdio.h>

// Print the pascal triangle of given size
void pascal_triangle(int size)
{
    // Variables that are the control executors of this function
    int i = 0, j = 0, k = 0;
    for (i = 0; i < size; i++)
    {
        //Print initial white space
        for (j = i + 1; j < size; j++)
        {
            printf("  ");
        }
        for (j = 0; j <= i; j++)
        {
            if (j == 0 || i == 0)
            {
                //Set initial values
                k = 1;
            }
            else
            {
                //Pascal's triangle number calculation
                k = k *(i - j + 1) / j;
            }
            //Display the value of calculate number
            printf(" %d  ", k);
        }
        printf("\n");
    }
}
int main()
{
    //Test Case
    pascal_triangle(5);
    pascal_triangle(7);
    return 0;
}

Output

         1
       1   1
     1   2   1
   1   3   3   1
 1   4   6   4   1
             1
           1   1
         1   2   1
       1   3   3   1
     1   4   6   4   1
   1   5   10   10   5   1
 1   6   15   20   15   6   1
package main
import "fmt"
/*
  Go Program
  Pascal's triangle patterns
*/

// Print the pascal triangle of given size
func pascalTriangle(size int) {
    // Variables that are the control executors of this function
    var i int = 0
    var j int = 0
    var k int = 0
    for i = 0 ; i < size ; i++ {
        // Print initial white space
        for j = i + 1 ; j < size ; j++ {
            fmt.Print("  ")
        }
        for j = 0 ; j <= i ; j++ {
            if j == 0 || i == 0 {
                // Set initial values
                k = 1
            } else {
                // Pascal's triangle number calculation
                k = k * (i - j + 1) / j
            }
            // Display the value of calculate number
            fmt.Print(" ", k, "  ")
        }
        fmt.Print("\n")
    }
    fmt.Print("\n")
}
func main() {
    
    // Test Case
    pascalTriangle(5)
    pascalTriangle(7)
}

Output

         1
       1   1
     1   2   1
   1   3   3   1
 1   4   6   4   1

             1
           1   1
         1   2   1
       1   3   3   1
     1   4   6   4   1
   1   5   10   10   5   1
 1   6   15   20   15   6   1
// Include namespace system
using System;
/*
  Csharp Program
  Pascal's triangle patterns
*/
public class PascalPattern
{
    // Print the pascal triangle of given size
    public void pascalTriangle(int size)
    {
        // Variables that are the control executors of this function
        int i = 0;
        int j = 0;
        int k = 0;
        for (i = 0; i < size; i++)
        {
            // Print initial white space
            for (j = i + 1; j < size; j++)
            {
                Console.Write("  ");
            }
            for (j = 0; j <= i; j++)
            {
                if (j == 0 || i == 0)
                {
                    // Set initial values
                    k = 1;
                }
                else
                {
                    // Pascal's triangle number calculation
                    k = k * (i - j + 1) / j;
                }
                // Display the value of calculate number
                Console.Write(" " + k + "  ");
            }
            Console.Write("\n");
        }
        Console.Write("\n");
    }
    public static void Main(String[] args)
    {
        PascalPattern task = new PascalPattern();
        // Test Case
        task.pascalTriangle(5);
        task.pascalTriangle(7);
    }
}

Output

         1
       1   1
     1   2   1
   1   3   3   1
 1   4   6   4   1

             1
           1   1
         1   2   1
       1   3   3   1
     1   4   6   4   1
   1   5   10   10   5   1
 1   6   15   20   15   6   1
<?php
/*
  Php Program
  Pascal's triangle patterns
*/
class PascalPattern
{
    // Print the pascal triangle of given size
    public  function pascalTriangle($size)
    {
        // Variables that are the control executors of this function
        $i = 0;
        $j = 0;
        $k = 0;
        for ($i = 0; $i < $size; $i++)
        {
            // Print initial white space
            for ($j = $i + 1; $j < $size; $j++)
            {
                print_r("  ");
            }
            for ($j = 0; $j <= $i; $j++)
            {
                if ($j == 0 || $i == 0)
                {
                    // Set initial values
                    $k = 1;
                }
                else
                {
                    // Pascal's triangle number calculation
                    $k = (int)($k * ($i - $j + 1) / $j);
                }
                // Display the value of calculate number
                print_r(" ".strval($k)."  ");
            }
            print_r("\n");
        }
        print_r("\n");
    }
    public static function main($args)
    {
        $task = new PascalPattern();
        // Test Case
        $task->pascalTriangle(5);
        $task->pascalTriangle(7);
    }
}
PascalPattern::main(array());

Output

         1
       1   1
     1   2   1
   1   3   3   1
 1   4   6   4   1

             1
           1   1
         1   2   1
       1   3   3   1
     1   4   6   4   1
   1   5   10   10   5   1
 1   6   15   20   15   6   1
/*
  Node JS Program
  Pascal's triangle patterns
*/
class PascalPattern
{
    // Print the pascal triangle of given size
    pascalTriangle(size)
    {
        // Variables that are the control executors of this function
        var i = 0;
        var j = 0;
        var k = 0;
        for (i = 0; i < size; i++)
        {
            // Print initial white space
            for (j = i + 1; j < size; j++)
            {
                process.stdout.write("  ");
            }
            for (j = 0; j <= i; j++)
            {
                if (j == 0 || i == 0)
                {
                    // Set initial values
                    k = 1;
                }
                else
                {
                    // Pascal's triangle number calculation
                    k = parseInt(k * (i - j + 1) / j);
                }
                // Display the value of calculate number
                process.stdout.write(" " + k + "  ");
            }
            process.stdout.write("\n");
        }
        process.stdout.write("\n");
    }
}

function main()
{
    var task = new PascalPattern();
    // Test Case
    task.pascalTriangle(5);
    task.pascalTriangle(7);
}
main();

Output

         1
       1   1
     1   2   1
   1   3   3   1
 1   4   6   4   1

             1
           1   1
         1   2   1
       1   3   3   1
     1   4   6   4   1
   1   5   10   10   5   1
 1   6   15   20   15   6   1
#  Python 3 Program
#  Pascal's triangle patterns
class PascalPattern :
    #  Print the pascal triangle of given size
    def pascalTriangle(self, size) :
        #  Variables that are the control executors of this function
        i = 0
        j = 0
        k = 0
        i = 0
        while (i < size) :
            j = i + 1
            #  Print initial white space
            while (j < size) :
                print("  ", end = "")
                j += 1
            
            j = 0
            while (j <= i) :
                if (j == 0 or i == 0) :
                    #  Set initial values
                    k = 1
                else :
                    #  Pascal's triangle number calculation
                    k = int(k * (i - j + 1) / j)
                
                #  Display the value of calculate number
                print("", k ," ", end = "")
                j += 1
            
            print(end = "\n")
            i += 1
        
        print(end = "\n")
    

def main() :
    task = PascalPattern()
    #  Test Case
    task.pascalTriangle(5)
    task.pascalTriangle(7)

if __name__ == "__main__": main()

Output

         1
       1   1
     1   2   1
   1   3   3   1
 1   4   6   4   1

             1
           1   1
         1   2   1
       1   3   3   1
     1   4   6   4   1
   1   5   10   10   5   1
 1   6   15   20   15   6   1
#  Ruby Program
#  Pascal's triangle patterns
class PascalPattern 
    #  Print the pascal triangle of given size
    def pascalTriangle(size) 
        #  Variables that are the control executors of this function
        i = 0
        j = 0
        k = 0
        i = 0
        while (i < size) 
            j = i + 1
            #  Print initial white space
            while (j < size) 
                print("  ")
                j += 1
            end

            j = 0
            while (j <= i) 
                if (j == 0 || i == 0) 
                    #  Set initial values
                    k = 1
                else
 
                    #  Pascal's triangle number calculation
                    k = k * (i - j + 1) / j
                end

                #  Display the value of calculate number
                print(" ", k ,"  ")
                j += 1
            end

            print("\n")
            i += 1
        end

        print("\n")
    end

end

def main() 
    task = PascalPattern.new()
    #  Test Case
    task.pascalTriangle(5)
    task.pascalTriangle(7)
end

main()

Output

         1  
       1   1  
     1   2   1  
   1   3   3   1  
 1   4   6   4   1  

             1  
           1   1  
         1   2   1  
       1   3   3   1  
     1   4   6   4   1  
   1   5   10   10   5   1  
 1   6   15   20   15   6   1  

/*
  Scala Program
  Pascal's triangle patterns
*/
class PascalPattern()
{
    // Print the pascal triangle of given size
    def pascalTriangle(size: Int): Unit = {
        // Variables that are the control executors of this function
        var i: Int = 0;
        var j: Int = 0;
        var k: Int = 0;
        while (i < size)
        {
            j = i + 1;
            // Print initial white space
            while (j < size)
            {
                print("  ");
                j += 1;
            }
            j = 0;
            while (j <= i)
            {
                if (j == 0 || i == 0)
                {
                    // Set initial values
                    k = 1;
                }
                else
                {
                    // Pascal's triangle number calculation
                    k = k * (i - j + 1) / j;
                }
                // Display the value of calculate number
                print(" " + k + "  ");
                j += 1;
            }
            print("\n");
            i += 1;
        }
        print("\n");
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var task: PascalPattern = new PascalPattern();
        // Test Case
        task.pascalTriangle(5);
        task.pascalTriangle(7);
    }
}

Output

         1
       1   1
     1   2   1
   1   3   3   1
 1   4   6   4   1

             1
           1   1
         1   2   1
       1   3   3   1
     1   4   6   4   1
   1   5   10   10   5   1
 1   6   15   20   15   6   1
/*
  Kotlin Program
  Pascal's triangle patterns
*/
class PascalPattern
{
    // Print the pascal triangle of given size
    fun pascalTriangle(size: Int): Unit
    {
        // Variables that are the control executors of this function
        var i: Int = 0;
        var j: Int ;
        var k: Int = 0;
        while (i < size)
        {
            j = i + 1;
            // Print initial white space
            while (j < size)
            {
                print("  ");
                j += 1;
            }
            j = 0;
            while (j <= i)
            {
                if (j == 0 || i == 0)
                {
                    // Set initial values
                    k = 1;
                }
                else
                {
                    // Pascal's triangle number calculation
                    k = k * (i - j + 1) / j;
                }
                // Display the value of calculate number
                print(" " + k + "  ");
                j += 1;
            }
            print("\n");
            i += 1;
        }
        print("\n");
    }
}
fun main(args: Array < String > ): Unit
{
    val task: PascalPattern = PascalPattern();
    // Test Case
    task.pascalTriangle(5);
    task.pascalTriangle(7);
}

Output

         1
       1   1
     1   2   1
   1   3   3   1
 1   4   6   4   1

             1
           1   1
         1   2   1
       1   3   3   1
     1   4   6   4   1
   1   5   10   10   5   1
 1   6   15   20   15   6   1
/*
  Swift 4 Program
  Pascal's triangle patterns
*/
class PascalPattern
{
    // Print the pascal triangle of given size
    func pascalTriangle(_ size: Int)
    {
        // Variables that are the control executors of this function
        var i: Int = 0;
        var j: Int = 0;
        var k: Int = 0;
        i = 0;
        while (i < size)
        {
            j = i + 1;
            // Print initial white space
            while (j < size)
            {
                print("  ", terminator: "");
                j += 1;
            }
            j = 0;
            while (j <= i)
            {
                if (j == 0 || i == 0)
                {
                    // Set initial values
                    k = 1;
                }
                else
                {
                    // Pascal's triangle number calculation
                    k = k * (i - j + 1) / j;
                }
                // Display the value of calculate number
                print("", k ," ", terminator: "");
                j += 1;
            }
            print(terminator: "\n");
            i += 1;
        }
        print(terminator: "\n");
    }
}
func main()
{
    let task: PascalPattern = PascalPattern();
    // Test Case
    task.pascalTriangle(5);
    task.pascalTriangle(7);
}
main();

Output

         1
       1   1
     1   2   1
   1   3   3   1
 1   4   6   4   1

             1
           1   1
         1   2   1
       1   3   3   1
     1   4   6   4   1
   1   5   10   10   5   1
 1   6   15   20   15   6   1


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