Q:

Java Program to display the Pascal's triangle

0

Java Program to display the Pascal's triangle using for loop and if else statement

All Answers

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

import java.util.Scanner;
public class demo1 
{
  public static void main(String[] args)
  {
    int row,c=1,a,i,j;
    System.out.print("Input number of rows: ");
    Scanner input = new Scanner(System.in);
    row = input.nextInt();

    for(i=0;i<row;i++)
    {
        for(a=1;a<=row-i;a++)
        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.print("\n");
    }
   }
}

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

total answers (1)

Similar questions


need a help?


find thousands of online teachers now