Pyramid Examples In Java
Pyramid Example 1
public class Pyramid_Ex1
{
public static void main(String[] args)
{
for (int i =1; i<=5; i++)
{
for(int j=1; j<=i; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Output is
*
**
***
****
*****
**
***
****
*****
Pyramid Example 2
public class Pyramid_Ex2
{
public static void main(String[] args)
{
for (int i=1; i<=5; i++)
{
for(int j=5; j>=i; j--)
{
System.out.print("*");
}
System.out.println();
}
}
}
Output is
*****
****
***
**
*
****
***
**
*
Pyramid Example 3
public class Pyramid_Ex3
{
public static void main(String[] args)
{
for (int i=1; i<=5; i++)
{
for(int j=1; j<=i; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
Output is
1
12
123
1234
12345
12
123
1234
12345
Pyramid Example 4
public class Pyramid_Ex4
{
public static void main(String[] args)
{
for (int i=1; i<=5; i++)
{
for(int j=i; j>=5; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
Output is
12345
1234
123
12
1
1234
123
12
1
thanks for sharing
ReplyDelete