Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Java program to print pascal triangle

Posted on December 6, 2019September 25, 2022

Table of Contents

  • Java program to print pascal triangle
    • Code to print pascal triangle
      • Program to print pascal triangle Using for loop
      • Program to print Pascal triangle Using while loop
      • Program to print pascal triangle Using do-while loop

Java program to print pascal triangle

In this tutorial, we will discuss the Java program to print pascal triangle

In this post, we will learn how to display pascal triangle in java language using for, while and do-while loop

Java program to print pascal triangle
Pascal triangle

Code to print pascal triangle

Program to print pascal triangle Using for loop

Program 1

This program allows the user to enter the number of rows and it will display pascal triangle number pattern using for loop in Java language

//Java programto print pascal pyramid number pattern
import java.util.Scanner;
class PascalTriangle{
public static void main (String args[]){
//create object for scanner
Scanner scan=new Scanner(System.in);
//Enter number ofrows for pascal triangle
System.out.print("Enter the row for pascal triangle: ");
int rows=scan.nextInt();
int count=1;//variable count initialize as 1
for(int i=0; i<rows; i++){
   for(int j=rows; j>i; j--){//inner for loop
   System.out.print(" ");
//first inner loop print space
   }
   count=1;
   for(int k=0; k<=i; k++){
   System.out.print(count+" ");
     count=count*(i-k)/(k+1);
//second inner loop prints number according to the given equation
  
   }
   System.out.println();

}
}
}

When the above code is executed, it produces the following result

Java program to display pascal triangle
Output

 

Program to print Pascal triangle Using while loop

Program 2

This program allows the user to enter the number of rows and it will be displayed pascal triangle number pattern using while loop in Java language.

//Java programto print pascal pyramid number pattern
import java.util.Scanner;
class PascalTrianglewhile{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
//create object for scanner
System.out.print("Enter the row for pascal triangle: ");
//Enter number ofrows for pascal triangle
int rows=scan.nextInt();
int count=1;
int i=0;
while( i<rows){
int j=rows;
   while(j>i){
   System.out.print(" ");  //first inner loop print space
    j--;
   }
   count=1;
   int k=0;
   while(k<=i){
   System.out.print(count+" ");
     count=count*(i-k)/(k+1);
//second inner loop prints number according to the given equation
   k++;
   }
   System.out.println();
i++;
}
}
}

When the above code is executed, it produces the following result

Java program to display pascal triangle
Pascal triangle pattern

Program to print pascal triangle Using do-while loop

Program 3

This program allows the user to enter the number of rows and it will be displayed pascal triangle number pattern using the do-while loop in Java language.

//Java programto print pascal pyramid number pattern
import java.util.Scanner;
class PascalTriangleDowhile{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
//create object for scanner
System.out.print("Enter the row for pascal triangle: ");
//Enter number ofrows for pascal triangle
int rows=scan.nextInt();
int count=1;
int i=0;
do{
int j=rows;
   do{
   System.out.print(" ");  //first inner loop print space
    j--;
   }while(j>i);
   count=1;
   int k=0;
  do{
   System.out.print(count+" ");
     count=count*(i-k)/(k+1);
//second inner loop prints number according to the given equation
   k++;
   } while(k<=i);
   System.out.println();
i++;
}while( i<rows);
}
}

When the above code is executed, it produces the following result

 

Suggested for you

For loop in Java               while loop in Java             Do while loop in Java

Nested for loop in Java     Nested while loop in Java

if statement in Java        Data type and variable in Java  operator in Java

 

Similar post

C program to print pascal triangle

C++ program to print pascal triangle

Java program to triangle number pattern

Java program to triangle number pattern using while loop

Java program to pyramid triangle star pattern

 

 

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes