Page Contents
- 1 Pyramid pattern programs in Java that Most asked in Interview
- 1.1 And below c concepts are used to print that patterns
- 1.2 1). Program to print half pyramid pattern using star(*) in Java
- 1.3 2). Program to print inverted half pyramid pattern using star(*) in Java
- 1.4 3). Program to print right half pyramid pattern using star(*) in Java
- 1.5 4). Program to print inverted right half pyramid pattern using star(*) in Java
- 1.6 5). Program to print full pyramid pattern using star(*) in Java
- 1.7 6). Program to print inverted full pyramid pattern using star(*) in Java
- 1.8 7). Program to print half pyramid pattern using numbers in Java
- 1.9 8). Program to print inverted half pyramid pattern using numbers in Java
- 1.10 Also prepare these Java Pattern Programs:
Interview Content
- C Programming Coding Questions
- C Pattern Programming Questions
- C Programming Interview Questions
- Java Programming Coding Questions
- Java Pattern Programming Questions
- Java Programming Interview Questions
- Python Programming Coding Questions
- Python Pattern Programming Questions
- Python Programming Interview Questions
- SQL Interview Questions
Pyramid pattern programs in Java that Most asked in Interview
Ans:
And below c concepts are used to print that patterns
- For Loop
- While Loop
- if..else
1). Program to print half pyramid pattern using star(*) in Java
import java.util.*;
class Main{
public static void main(String ...a){
int i=0,temp=0,j;
Scanner sc= new Scanner(System.in);
System.out.print("Enter the number of rows for half pyramid: ");
int n= sc.nextInt();
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
System.out.print("*");
}
System.out.println("");
}
}
}
Output
2). Program to print inverted half pyramid pattern using star(*) in Java
import java.util.*;
class Main{
public static void main(String ...a){
int i=0,temp=0,j;
Scanner sc= new Scanner(System.in);
System.out.print("Enter the number of rows for half pyramid: ");
int n= sc.nextInt();
for(i=n;i>=1;i--){
for(j=i;j>=1;j--){
System.out.print("*");
}
System.out.println("");
}
}
}
Output
3). Program to print right half pyramid pattern using star(*) in Java
import java.util.*;
class Main{
public static void main(String ...a){
int i=0,temp=0,j,k;
Scanner sc= new Scanner(System.in);
System.out.print("Enter the number of rows for half pyramid: ");
int n= sc.nextInt();
for(i=n;i>=1;i--){
for(j=i;j>=2;j--){
System.out.print(" ");
}
for(k=1;k<=n-i+1;k++){
System.out.print("*");
}
System.out.println("");
}
}
}
Output
4). Program to print inverted right half pyramid pattern using star(*) in Java
import java.util.*;
class Main{
public static void main(String ...a){
int i=0,temp=0,j,k;
Scanner sc= new Scanner(System.in);
System.out.print("Enter the number of rows for half pyramid: ");
int n= sc.nextInt();
for(i=n;i>=1;i--){
for(j=1;j<=n-i;j++){
System.out.print(" ");
}
for(k=1;k<=i;k++){
System.out.print("*");
}
System.out.println("");
}
}
}
Output
5). Program to print full pyramid pattern using star(*) in Java
import java.util.*;
class Main{
public static void main(String ...a){
int i=0,temp=0,j,k;
Scanner sc= new Scanner(System.in);
System.out.print("Enter the number of rows for pyramid: ");
int n= sc.nextInt();
for(i=n;i>=1;i--){
for(j=i;j>=2;j--){
System.out.print(" ");
}
for(k=1;k<=2*(n-i+1)-1;k++){
System.out.print("*");
}
System.out.println("");
}
}
}
Output
6). Program to print inverted full pyramid pattern using star(*) in Java
import java.util.*;
class Main{
public static void main(String ...a){
int i=0,temp=0,j,k;
Scanner sc= new Scanner(System.in);
System.out.print("Enter rows for Inv. Full Pyramid Pattern: ");
int n= sc.nextInt();
for(i=n;i>=1;i--){
for(j=1;j<=n-i;j++){
System.out.print(" ");
}
for(k=1;k<=2*i-1;k++){
System.out.print("*");
}
System.out.println("");
}
}
}
Output
7). Program to print half pyramid pattern using numbers in Java
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,k;
printf("Enter no. of rows for half pyramid: ");
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
printf("%d",j);
}
printf("\n");
}
getch();
}
Output
8). Program to print inverted half pyramid pattern using numbers in Java
import java.util.*;
class Main{
public static void main(String ...a){
int i=0,temp=0,j,k;
Scanner sc= new Scanner(System.in);
System.out.print("Enter the number of rows for half pyramid: ");
int n= sc.nextInt();
for(i=0;i<n;i++){
for(j=1;j<=n-i;j++){
System.out.print(j);
}
System.out.println("");
}
}
}
Output
Latest Uploads on Website
- AVL Tree with explanation
- Radix sort algorithm explanation with example
- Quick Sort Algorithm with explanation
- Bubble sorting algorithm with Bubble sort program in C
- Insertion sort algorithm and program in C
- Selection Sort Algorithm and Program in C
- Linear probing technique explanation with example
- Collision in Hashing and Collision resolution technique
- Hashing in data structure with its types
- Binary search tree operations with Program
- Binary search tree in data structure
- Binary search algorithm in data structure with explanation
- linear search in data structure with Algo and Program