Q:

Program to print the following pattern (pascal triangle)

belongs to collection: Pattern Programs

0

Algorithm

  1. Start
  2. Let i be an integer number.
  3. Let j be an integer number.
  4. Let row be an integer number and initialize by 6.
  5. Let coe be an integer number and initialize by 0.
  6. Repeat step 7 to 14 until all value parsed.
  7. Set i = 0 and check I <= row;
  8. Set j = 1 and check j <= row-i;
  9. Print space.
  10. check if j==0 || i==0
  11. set coe = 1;
  12. Else
  13. Set coe = coe * (i+j-1)/j;
  14. Print coe.
  15. End

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

JAVA

package programs;  
public class Pattern11   
{  
    public static void main(String[] args)   
    {  
        int coe=1,rows = 6;  
     for(int i = 0; i < rows; i++) {  
            for(int space = 1; space < rows - i; ++space) {  
                System.out.print("  ");  
            }  
  
            for(int j = 0; j <= i; j++) {  
                if (j == 0 || i == 0)  
                    coe = 1;  
                else  
                    coe = coe * (i - j + 1) / j;  
  
                System.out.printf("%4d", coe);  
            }  
      System.out.println();  
        }     
    }  
   }  

 

Python

numberOfRows = 7  
def formatNumber(number):  
  if number<10:  
    return "  "+str(number) + " "  
  else:  
    return " " + str(number) + " "  
row = [1]  
print("  " * 7 + formatNumber(row[0]))  
row.append(1)  
print("  " * 6 + formatNumber(row[0]) + formatNumber(row[1]))  
row.append(2)  
print("  " * 5 + formatNumber(row[0]) + formatNumber(row[1]+row[0]) + formatNumber(row[1]))  
row.append(3)  
print("  " * 4 + formatNumber(row[0]) + formatNumber(row[3]) +formatNumber(row[3]) + formatNumber(row[1]))  
row.append(4)  
print("  " * 3 + formatNumber(row[0]) + formatNumber(row[4]) + formatNumber(row[4]+row[2]) +formatNumber(row[4]) + formatNumber(row[1]))  
row.append(5)  
print("  " * 2 + formatNumber(row[0]) + formatNumber(row[5]) + formatNumber(row[5]+row[5]) + formatNumber(row[5]+row[5]) +formatNumber(row[5]) + formatNumber(row[1]))  

 

C program

#include <stdio.h>  
int main()  
{  
 int co=1,rows = 6;  
for(int i = 0; i < rows; i++)  
{  
    for(int space = 1; space < rows - i; ++space)  
    {  
        printf("  ");  
    }  
    for(int j = 0; j <= i; j++)   
    {  
        if (j == 0 || i == 0)  
            co = 1;  
        else  
            co = co * (i - j + 1) / j;  
        printf("%4d", co);  
    }  
printf("\n");  
}  
}  

 

C# program

using System;  
public class Program  
{  
public static void Main()  
{  
 int row, i, j, k;  
 row = 5;  
        for (i = 0; i <= row; i++)  
        {  
        k = 1;  
        for (j = i; j <= row - 1; j++)  
        Console.Write(" ");  
   
        for (j = 0; j <= i; j++)  
        {  
        Console.Write("{0} ", k);  
        k = (k * (i - j) / (j + 1));  
        }  
        Console.WriteLine();  
        }  
 Console.ReadLine();    
}  
}  

 

PHP Program

<?php  
  $co=1;  
$rows = 6;  
for($i = 0; $i < $rows; $i++)  
{  
    for($space = 1; $space < $rows - $i; ++$space)  
    {  
        echo " ";  
    }  
    for($j = 0; $j <= $i; $j++)   
    {  
        if ($j == 0 || $i == 0)  
            $co = 1;  
        else  
            $co = $co * ($i - $j + 1) / $j;  
        echo $co." ";  
    }  
  echo "<br>";  
}  
?>  

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Program to print the following pattern... >>
<< Program to print the following pattern...