print the following pattern using java programming,
PYRAMID FORM...
*
* *
* * *
* * * *
Answers
Explanation:
public class Main {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; ++i) {
for (int j = 1; j <= i; ++j) {
System.out.print("* ");
}
System.out.println();
}
}
}
Example 2: Program to print half pyramid a using numbers
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Source Code
public class Main {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; ++i) {
for (int j = 1; j <= i; ++j) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
Example 3: Program to print half pyramid using alphabets
A
B B
C C C
D D D D
E E E E E
Source Code
public class Main {
public static void main(String[] args) {
char last = 'E', alphabet = 'A';
for (int i = 1; i <= (last - 'A' + 1); ++i) {
for (int j = 1; j <= i; ++j) {
System.out.print(alphabet + " ");
}
++alphabet;
System.out.println();
}
}
}
Programs to print inverted half pyramid using * and numbers
As it is mentioned Pyramid form....The correct pattern is given below
Code Language:-
- Java
Code:-
package Coder;
public class PyramidStarPatt
{
public static void main (String ar [])
{
for(int i=1;i<4;i++)
{
for(int j=4;j>i;j--)
{
System.out.print(" "); //This loop prints the spaces
}
for(int j=1;j<=i;j++)
{
System.out.print("* ");
//This loop prints asterisk(*) with spaces
}
System.out.println(); //To leave lines
}
}
}
Variable Description:-
- i:- loop variable
- j:- Loop variable