How To Create Pyramid Of Numbers In Java?
This is one of the frequently asked java interview question for
freshers. This question tests the candidate’s logical ability as well as
the basic understanding of Java language. They can ask you to create
pyramid of numbers with different patterns like in the below image.
import java.util.*;
class Pattern
{
public static void pattern1(int row) //Pattern1
{
int count = 1;
for(int i=row; i>0; i--)
{
for(int j=1; j<=i; j++)
System.out.print(" ");
for(int j=1; j<=count; j++)
System.out.print(count+" ");
System.out.println();
count++;
}
}
public static void pattern2(int row) //Pattern2
{
int count = 1;
for(int i=row; i>0; i--)
{
for(int j=1; j<=i; j++)
System.out.print(" ");
for(int j=1; j<=count; j++)
{
System.out.print(j +" ");
}
System.out.println();
count++;
}
}
public static void pattern3(int row) //Pattern 3
{
int count = 1;
for(int i=row; i>0; i--)
{
for(int j=1; j<=i; j++)
System.out.print(" ");
for(int j=1; j<=count; j++)
System.out.print("*"+" ");
System.out.println();
count++;
}
}
public static void pattern4(int row) //Pattern 4
{
int count = 1;
for(int i=row; i>0; i--)
{
for(int j=1; j<=i*2; j++)
System.out.print(" ");
for(int j=1; j<=count; j++)
System.out.print(j+" ");
for(int j=count-1; j>=1; j--)
System.out.print(j+" ");
System.out.println();
count++;
}
}
public static void pattern5(int row) //Pattern5
{
int count = row;
for(int i=0; i<row; i++)
{
for(int j=1; j<=i*2; j++)
System.out.print(" ");
for(int j=1; j<=count; j++)
System.out.print(j+" ");
for(int j=count-1; j>=1; j--)
System.out.print(j+" ");
System.out.println();
count--;
}
}
public static void pattern6(int row) //Pattern6
{
int count = 1;
for(int i=row; i>0; i--)
{
for(int j=1; j<=i*2; j++)
System.out.print(" ");
for(int j=i; j<=row; j++)
System.out.print(j+" ");
for(int j=row-1; j>=i; j--)
System.out.print(j+" ");
System.out.println();
count++;
}
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
Pattern p= new Pattern();
p.pattern1(row);
p.pattern2(row);
p.pattern3(row);
p.pattern4(row);
System.out.println();
p.pattern5(row);
System.out.println();
p.pattern6(row);
}
}
class Pattern
{
public static void pattern1(int row) //Pattern1
{
int count = 1;
for(int i=row; i>0; i--)
{
for(int j=1; j<=i; j++)
System.out.print(" ");
for(int j=1; j<=count; j++)
System.out.print(count+" ");
System.out.println();
count++;
}
}
public static void pattern2(int row) //Pattern2
{
int count = 1;
for(int i=row; i>0; i--)
{
for(int j=1; j<=i; j++)
System.out.print(" ");
for(int j=1; j<=count; j++)
{
System.out.print(j +" ");
}
System.out.println();
count++;
}
}
public static void pattern3(int row) //Pattern 3
{
int count = 1;
for(int i=row; i>0; i--)
{
for(int j=1; j<=i; j++)
System.out.print(" ");
for(int j=1; j<=count; j++)
System.out.print("*"+" ");
System.out.println();
count++;
}
}
public static void pattern4(int row) //Pattern 4
{
int count = 1;
for(int i=row; i>0; i--)
{
for(int j=1; j<=i*2; j++)
System.out.print(" ");
for(int j=1; j<=count; j++)
System.out.print(j+" ");
for(int j=count-1; j>=1; j--)
System.out.print(j+" ");
System.out.println();
count++;
}
}
public static void pattern5(int row) //Pattern5
{
int count = row;
for(int i=0; i<row; i++)
{
for(int j=1; j<=i*2; j++)
System.out.print(" ");
for(int j=1; j<=count; j++)
System.out.print(j+" ");
for(int j=count-1; j>=1; j--)
System.out.print(j+" ");
System.out.println();
count--;
}
}
public static void pattern6(int row) //Pattern6
{
int count = 1;
for(int i=row; i>0; i--)
{
for(int j=1; j<=i*2; j++)
System.out.print(" ");
for(int j=i; j<=row; j++)
System.out.print(j+" ");
for(int j=row-1; j>=i; j--)
System.out.print(j+" ");
System.out.println();
count++;
}
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
Pattern p= new Pattern();
p.pattern1(row);
p.pattern2(row);
p.pattern3(row);
p.pattern4(row);
System.out.println();
p.pattern5(row);
System.out.println();
p.pattern6(row);
}
}
error compiling
ReplyDeleteI have tested, works without any error
Delete