Description
Get the number of rows as input from the user and print the Pascal triangle as given below.
Input
3
Output
1
1 1
1 2 1
C Program
#include <stdio.h>
int main()
{
int n,c=1,k,i,j;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(k=1;k<=n-i;k++)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
if (j==0||i==0)
c=1;
else
c=c*(i-j+1)/j;
printf("%d ",c);
}
printf("\n");
}
return 0;
}
C++ Program
#include <iostream>
using namespace std;
int main()
{
int n,c=1,k,i,j;
cout<<"Enter the number of rows: ";
cin>>n;
for(i=0;i<n;i++)
{
for(k=1;k<=n-i;k++)
{
cout<<" ";
}
for(j=0;j<=i;j++)
{
if (j==0||i==0)
c=1;
else
c=c*(i-j+1)/j;
cout<<c<<" ";
}
cout<<"\n";
}
return 0;
}
Java Program
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n,c=1,k,i,j;
System.out.print("Enter the number of rows: ");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for(i=0;i<n;i++)
{
for(k=1;k<=n-i;k++)
System.out.print(" ");
for(j=0;j<=i;j++)
{
if (j==0||i==0)
c=1;
else
c=c*(i-j+1)/j;
System.out.print(" "+c);
}
System.out.println();
}
}
}
Python Program
row=int(input("Enter the number of rows: "))
for i in range(0,row):
c=1
for k in range(1,row-i):
print(" ",end="")
for j in range(0,i+1):
print(" ",c,end="")
c=c*(i-j)//(j+1)
print()