C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program to print the following pattern (pascal triangle)Algorithm
JAVApackage 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(); } } } PythonnumberOfRows = 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# programusing 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>"; } ?>
Next TopicProgram to Print pattern 16
|