In this page, we will understand and learn different star and number java pattern programs. These pattern programs frequently asked in written Test and Technical interview to test your programming skills and way of your logic you applied to solve real time problems. Having knowledge on for loop,nested loop and while loop very useful to solve pattern programs in java. Let us understand with pattern programs how to use for loop and nested for loop in our pattern programs
1) How to print Star pyramid pattern in Java?
class StarPattern
{
public static void main(String[] args)
{
for(int i=0;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println();
}
}//this is end of main() method
}//this is end of class
Output:
2) How to print star pyramid pattern like below output:
class StarPattern1
{
public static void main(String[] args)
{
for(int i=0;i<=5;i++)
{
for(int j=5;j>=i;j--)
{
System.out.print("*");
}
System.out.println();
}
}
}//this is end of class
Output:
3) How to print Star pyramid pattern like below result?
class StarPattern2
{
public static void main(String[] args)
{
int n=7;
int z=7;
for(int i=1;i<=n;i++)
{
for(int j=1;j<i;j++)
{
System.out.print(" ");
}
for(int k=1;k<=z;k++)
{
System.out.print("*");
}
z--;
System.out.println();
}
}
}
Output:
5) How to print Star Pyramid pattern like below result?
class StarPattern3
{
public static void main(String[] args)
{
int size=5;
for(int i=size;i>=-size;i--)
{
for(int j=size;j>=Math.abs(i);j--)
{
System.out.print("*");
}
System.out.println();
}
}
}
Output:
6) How to print Number Pyramid in java like below result?
class NumberPattern
{
public static void main(String[] args)
{
int n=7;
int z=1;
for(int i=0;i<n;i++)
{
for(int j=n-1;j>i;j--)
{
System.out.print(" ");
}
for(int k=1;k<=z;k++)
{
System.out.print(k);
}
z=z+2;
System.out.println();
}
}
}
Output:
7) How to print Number Pyramid in java like below result?
class NumberPattern1
{
public static void main(String[] args)
{
int size=5;
int i,j,k;
for(i=size;i>=-size;i--)
{
for(j=1;j<=Math.abs(i);j++)
{
System.out.print(" ");
}
for(k=Math.abs(i);k<=size;k++)
{
System.out.print(k);
}
System.out.print("\n");
}
}
}
Output:
8) How to print Number Pyramid pattern in java like below result?
class NumberPattern2
{
public static void main(String[] args)
{
for(int i=1;i<=5;i++)
{
for(int k=4;k>=i;k--)
{
System.out.print(" ");
}
for(int j=1;j<=i;j++)
{
System.out.print(i+" ");
}
System.out.println();
}
}
}
Output:
9) How to Print Number Pyramid pattern in java like below result?
class NumberPattern3
{
public static void main(String[] args)
{
int n=5;
int z=1;
for(int i=0;i<n;i++)
{
for(int j=n-1;j>i;j--)
{
System.out.println("");
}
for(int k=1;k<=z;k++)
{
System.out.print(z);
}
z=z+2;
System.out.println();
}
}
}
Output:
1) How to print Star pyramid pattern in Java?
class StarPattern
{
public static void main(String[] args)
{
for(int i=0;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println();
}
}//this is end of main() method
}//this is end of class
Output:
2) How to print star pyramid pattern like below output:
class StarPattern1
{
public static void main(String[] args)
{
for(int i=0;i<=5;i++)
{
for(int j=5;j>=i;j--)
{
System.out.print("*");
}
System.out.println();
}
}
}//this is end of class
Output:
3) How to print Star pyramid pattern like below result?
class StarPattern2
{
public static void main(String[] args)
{
int n=7;
int z=7;
for(int i=1;i<=n;i++)
{
for(int j=1;j<i;j++)
{
System.out.print(" ");
}
for(int k=1;k<=z;k++)
{
System.out.print("*");
}
z--;
System.out.println();
}
}
}
Output:
5) How to print Star Pyramid pattern like below result?
class StarPattern3
{
public static void main(String[] args)
{
int size=5;
for(int i=size;i>=-size;i--)
{
for(int j=size;j>=Math.abs(i);j--)
{
System.out.print("*");
}
System.out.println();
}
}
}
Output:
6) How to print Number Pyramid in java like below result?
class NumberPattern
{
public static void main(String[] args)
{
int n=7;
int z=1;
for(int i=0;i<n;i++)
{
for(int j=n-1;j>i;j--)
{
System.out.print(" ");
}
for(int k=1;k<=z;k++)
{
System.out.print(k);
}
z=z+2;
System.out.println();
}
}
}
Output:
7) How to print Number Pyramid in java like below result?
class NumberPattern1
{
public static void main(String[] args)
{
int size=5;
int i,j,k;
for(i=size;i>=-size;i--)
{
for(j=1;j<=Math.abs(i);j++)
{
System.out.print(" ");
}
for(k=Math.abs(i);k<=size;k++)
{
System.out.print(k);
}
System.out.print("\n");
}
}
}
Output:
8) How to print Number Pyramid pattern in java like below result?
class NumberPattern2
{
public static void main(String[] args)
{
for(int i=1;i<=5;i++)
{
for(int k=4;k>=i;k--)
{
System.out.print(" ");
}
for(int j=1;j<=i;j++)
{
System.out.print(i+" ");
}
System.out.println();
}
}
}
Output:
9) How to Print Number Pyramid pattern in java like below result?
class NumberPattern3
{
public static void main(String[] args)
{
int n=5;
int z=1;
for(int i=0;i<n;i++)
{
for(int j=n-1;j>i;j--)
{
System.out.println("");
}
for(int k=1;k<=z;k++)
{
System.out.print(z);
}
z=z+2;
System.out.println();
}
}
}
Output:
10) How to Print Number Pyramid pattern in Java like below result?
class NumberPattern4
{
public static void main(String[] args)
{
int size=3;
int i,j;
for(i=size;i>=-size;i--)
{
for(j=size;j>=Math.abs(i);j--)
{
System.out.print(j);
}
System.out.println("\n");
}
}
}
Output:
No comments:
Post a Comment