For Loop

# Java Pyramid 5 Example

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

• mahesh says:

Hi,

I need a code for
1
12
123
1234

• paras1598 says:

import java.util.Scanner;
class prc2
{
public static void main(String arg[])
{
Scanner a=new Scanner(System.in);
int n=a.nextInt();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(” ” +j);
}
System.out.print(“\n”);
}
}

• mahesh says:

Thank You,
Can u please send one more code to me.

…1
12
.123
1234

54321
321
1

• Abhra Kar says:

for (int i = 5; i >= 1; i = i – 2) {
for (int j = i; j >= 1; j–) {
System.out.print(j);
}
System.out.println();
}

• Nivetha B says:

i need code
1 2 3 4 5
6 7 8
9
10 11 12
13 14 15 16 17

• Abhra Kar says:

int num = 1;
int row = 0;
int j = 5;

for (; j >= 1; j = j – 2) {
for (int k = 1; k <= j; k++) {
System.out.print(num);
num++;
}
System.out.println();
row++;
}

j = row;

for (; j <= 5; j = j + 2) {
for (int l = 1; l <= j; l++) {
System.out.print(num);
num++;
}
System.out.println();
}

• Jatin Narang says:

int num=1;
for(i=1;i<=5;i+=2)
{
System.out.println();
for(j=i;j<=5;j++)
{
System.out.print(num);
num++;

}
}

for(i=1;i<=2;i++)
{System.out.println();
for(j=2*i+1;j>=1;j–)
{
System.out.print(num);
num++;
}
}

please i a user to enter an integer from 1-15 to display in a pyramid form how..

• shivamjohn says:

{
Scanner input = new Scanner(System.in);

System.out.print(“Enter the number of lines: “);
int numberOfLines = input.nextInt();

if ((numberOfLines < 1) || (numberOfLines > 15))
{
System.out.println(“You must enter a number from 1 to 15”);
System.exit(0);
}

for (int row = 1; row <= numberOfLines; row++)
{
for (int column = 1; column <= numberOfLines – row; column++)
{
System.out.print(” “);
}
for (int num = row; num >= 1; num–)
{
System.out.print(” ” + num);
}
for (int num = 2; num <= row; num++)
{
System.out.print(” ” + num);
}

System.out.println(“”);
}
input.close();
}

• Ryan Carlo The Best says:

1
121
12321
1234321
123454321

• Satdhruti Paul says:

public class Helper
{
public static void main(String args[]){
String space;
for(int i = 1; i <= 5; i++){
space = “”;
for(int j = 1; j <=i; j++)space+=j;
for(int k = i-1; k>0;k–)space+=k;
System.out.println(space);
}
}
}

• m p prabhath says:

package pyromid;

public class prmdmodel6
{
public static void main(String args[]){

for(int i = 1; i <= 5; i++){

for(int j = 1; j <=i; j++)
System.out.print(j);
for(int k = i-1; k>0;k–)
System.out.print(k);
System.out.println();
}
}
}

• shivamjohn says:

What is the code for
1020304050
10203040
102030
1020
10

• Satdhruti Paul says:

public class xyz
{
public static void main(String args[]){
String space;
for(int i = 0; i <= 4; i++){
space=””;
for(int j = 1;j<=5-i;j++){
space+=j+”0″;
}
System.out.println(space);
}
}
}

• m p prabhath says:

/*
1020304050
10203040
102030
1020
10
*/

package pyromid;

public class prmdmodel5 {

public static void main(String[] args) {

for(int i=5;i>0;i–)
{
for(int j=1;j<=i;j++)
{
System.out.print(j*10);
}
System.out.println();
}
}

}

• shivamjohn says:

thank you

what is the code for
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15

….1
…2 6
..3 7 10
.4 8 11 13
5 9 12 14 15

• Satdhruti Paul says:

could u specify the pattern more clearly?

1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
in 1st row there are 4 space
in 2nd row 3 space
in 3rd row 2 space
4th row 1 space
5th row no space

• Satdhruti Paul says:

public class Helper
{
public static void main(String args[]){
String space;
int c = 0;
for(int i = 1; i <= 5; i++){
space=””;
space+=i+” “;
c=i;
for(int j = 1; j < i; j++){
space+=(c+(5-j))+” “;
c=c+(5-j);
}
System.out.println(space);
}
}
}

Thanku….

• Satdhruti Paul says:

• Sri Nath says:

space=”” and space+=i+””;
i can’t understand…

• Satdhruti Paul says:

let me write it more clearly
space = “”;
space += i + ” “; //not empty. It has space
/* If you look at the Radhesham’s pattern, the first element of the nth line is n. Take a look,
for the 1st line
1
-> 1st element of 1st line is 1
next line
2 6
-> 1st element of 2nd line is 2.
and so on.
In my loop, I use the counter i in the first loop for the number of lines(I hope thats clear).
Hope this helped!

• Satya says:

can u tell me code of it??
*
* *
* *
* *
*

• Swapnarani Behera says:

Write a program to draw the following patterns:

* * * * * * * * * * * * * * * *

* * * * * * * * * * * * * *

* * * * * * * * * * * *

* * * *

* *

• sairam velaga says:

public class Patt {
public static void main(String[] args) {
for (int i = 8; i>=1; i–) {
if(i==1||i==2||i==6||i==7||i==8)
{
for (int j = 1; j <=(i*2); j++) {
System.out.print(“*”);

}

System.out.println(“\n”);
}
}

}

}

• Syber Yabut says:

Can i know the sample code of this :

Sample Output:

Enter a Name: Atheena

Hello Atheena!!!

The first letter of your name is a vowel.

Ath

Ath Ath

Ath Ath Ath

Ath Ath Ath Ath

Ath Ath Ath Ath Ath

Try Again? [Y/N]: yes

Enter a Name: Raizel

Hello Raizel!!!

The first letter of your name is a consonant.

Rai Rai Rai Rai Rai

Rai Rai Rai Rai

Rai Rai Rai

Rai Rai

Rai

Try Again? [Y/N]: Yes

• sairam velaga says:

package sample;

import java.util.Scanner;

public class PattrenPrint {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println(“enter name …”);
String name=in.next();
if(name.charAt(0)==’a’||name.charAt(0)==’e’||name.charAt(0)==’i’||name.charAt(0)==’o’||name.charAt(0)==’u’){
char[] arr=name.toCharArray();

for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < i; j++) {
System.out.print(” “+name.substring(0,3));
}
System.out.println();
}

}
else{
char[] arr=name.toCharArray();

for (int i = arr.length; i >= 0; i–) {
for (int j = 0; j < i; j++) {
System.out.print(” “+name.substring(0,3));
}
System.out.println();
}
}
}

}

• sairam velaga says:

package sample;

import java.util.Scanner;

public class PattrenPrint {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println(“enter name …”);
String name=in.next();
System.out.println();
System.out.println(“Hello….! “+name);

if(name.charAt(0)==’a’||name.charAt(0)==’e’||name.charAt(0)==’i’||name.charAt(0)==’o’||name.charAt(0)==’u’||name.charAt(0)==’A’|name.charAt(0)==’E’||name.charAt(0)==’I’||name.charAt(0)==’O’||name.charAt(0)==’U’){
char[] arr=name.toCharArray();

for (int i = 1; i <= 5; i++) {
for (int j = 0; j < i; j++) {
System.out.print(” “+name.substring(0,3));
}
System.out.println();
}

}
else{
char[] arr=name.toCharArray();

for (int i = 5; i >=1; i–) {
for (int j = 0; j < i; j++) {
System.out.print(” “+name.substring(0,3));
}
System.out.println();
}
}
in.close();
}

}

• Sarthak Vishal says:

What is the code for :
COMPUTER
COMPUTE
COMPUT
COMPU
COMP
COM
CO
C

• kishan agarwal says:

public class Test
{

public static void main(String[] args)
{

String s=”string”;

char c[]=s.toCharArray();
for(int i=s.length();i>=1;i–){
for(int c1=0;c1<i;c1++){ system.out.print(c[c1]);}}}};=””>

• sairam velaga says:

public class StringPatt {
public static void main(String[] args) {
String patt=”COMPUTER”;

for (int i = patt.length()-1; i >=0; i–) {
for (int j = 0; j <=i; j++) {
char c=patt.charAt(j);
System.out.print(c);

}
System.out.println();

}
}

}

• m p prabhath says:

/*
computer
compute
comput
compu
comp
com
co
c
*/

package pyromid;

import java.util.Scanner;

public class prmdmodel3 {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println(“enter string”);
String st=sc.next();
char ch[]=st.toCharArray();

for(int i=ch.length;i>=0;i–)
{
for(int j=0;j<i;j++) {=”” system.out.print(ch[j]);=”” }=”” system.out.println();=”” }=”” }=”” }=””>

• Prem Bommidi says:

**
***
****
*****
******

can anyone please tell me the code for above pattern

• srinivas nangana says:

class AA
{
public static void main(String[] args)
{
for(int i=2;i<7;i++){
for(int j=0;j<i;j++){ system.out.print(“*”);=”” }=”” system.out.println(“”);}}}=””>

• Azran Azwer says:

what is the code for….
999999999
88888888
7777777
666666
55555
4444
333
22
1
1
22
333
4444
55555
666666
7777777
88888888
999999999

• Dhrubo Jyoti Ghosh says:

class Nested_For

{

public static void main(String args[])

{

for(int i=9;i>=1;i–)

{

for(int j=1;j<=i;j++)

{

System.out.print(i+” “);

}

System.out.println(” “);

}

for(int i=1;i<=9;i++)

{

for(int j=1;j<=i;j++)

{

System.out.print(i+” “);

}

System.out.println(“”);

}

}

}

• m p prabhath says:

public class pyromid {

public static void main(String[] args) {

for(int i=9;i>0;i–)

{

for(int j=0;j<i;j++) {=”” system.out.print(i);=”” }=”” system.out.println();=”” }=”” for(int=”” i=”1;i&lt;=9;i++)” {=”” for(int=”” j=”0;j&lt;i;j++)” {=”” system.out.print(i);=”” }=”” system.out.println();=”” }=”” }=”” }=””>

• sairam velaga says:

public class MyPatt {
public static void main(String[] args) {
for (int i = 9; i >=1; i–) {
for (int j = 1; j <=i; j++) {
System.out.print(i);
}
System.out.println();
}
for (int i = 1; i <=9; i++) {
for (int j = 1; j <=i; j++) {
System.out.print(i);
}
System.out.println();
}

}
}

• shruthi says:

class Down
{
public static void main(String args[])
{
for(int i=9;i>=1;i–)
{
System.out.println();
for(int j=1;j<=i;j++)
{
int k=i;
System.out.print(k);
}
}
for(int i=1;i<=9;i++)
{
System.out.println();
for(int j=1;j<=i;j++)
{
int h=i;
System.out.print(h);
}
}
}
}

• Sravani Priyanka says:

import java.util.Scanner;
public class Pyramid2 {

public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println(“Enter number of rows: “);
int rows=s.nextInt();

for(int i=rows;i>=0;i–)
{
for (int j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.print(“\n”);
}

for(int i=1;i<=rows;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();

}
}
}

• shobhit sharma says:

What will be the code for

0
0 1
0 2 4
0 3 6 9
0 4 8 16 20
0 5 10 15 20 15

• m p prabhath says:

package pyromid;

public class pyromidmodel8 {

public static void main(String[] args) {
// TODO Auto-generated method stub.

for(int i=0;i<=5;i++)
{
for(int j=0;j<=i;j++)
{
System.out.print(” “+j*i);
}
System.out.println();
}

}

}

• Manasa.s satturi says:

what will be the code for this?
12345 54321
1234 4321
123 321
12 21
1 1
12 21
123 321
1234 4321
12345 54321

• kanchan says:

package New_JT;

public class Pyramid1
{

public static void main(String[] args)
{
for (int j = 5; j >= 0; j–)
{
System.out.println(” “);

for (int i=1; i<=j; i++)
{
System.out.print(i);

}

System.out.print(” “);
for (int i=j; i>0; i–)
{
System.out.print(i);
}

}
for (int i = 2; i <= 5; i++)
{

for (int k=1; k<=i;k++)
{
System.out.print(k);
}
System.out.print(” “);
for (int m=i;m>0;m–)
{

System.out.print(m);
}
System.out.println();

}

}

}

• sairam velaga says:

public class Pyramid {
public static void main(String[] args) {
for (int i = 5; i >=1; i–) {
for (int j = 1; j <=i; j++) {
System.out.print(j);

}
System.out.print(” “);
for (int j1 = i; j1 >=1; j1–) {
System.out.print(j1);

}
System.out.println();

}
for (int i = 2; i <=5; i++) {
for (int j = 1; j <=i; j++) {
System.out.print(j);

}
System.out.print(” “);
for (int j1 = i; j1 >=1; j1–) {
System.out.print(j1);

}
System.out.println(” “);

}
}

}

• Manasa.s satturi says:

what will be the code for this?

12345 54321

1234 4321

123 321

12 21

1 1

12 21

123 321

1234 4321

12345 54321

• Akshaya Singh says:

int h=0;
for(int i=5; i>0 ;i–){

for(int j=1; j <= i; j++){
System.out.print(j);
h=j;
}
System.out.print(” “);
for (int j = 1; j<=i; j++) {
System.out.print(h–);
}
System.out.println(“”);
}
for(int i=2; i<= 5 ;i++){

for(int j=1; j <= i; j++){
System.out.print(j);
h=j;
}
System.out.print(” “);
for (int j = 1; j<=i; j++) {
System.out.print(h–);
}

System.out.println(“”);
}

• k.ramesh says:

• srinivasan says:

1
3 24 5
7 9 115 11 13

i need a code

• srinivasan says:

…………1
…..3…..24…….5
7…9…..115…..11……13

• Aran Sanchez says:

what is the code for this
54321
5432
543
54
5

*
* *
* * *
* * * *
* * * * *

• sairam velaga says:

public class MyPatt {
public static void main(String[] args) {
for (int i = 1; i<=5; i++) {
for (int j = 5; j>=i; j–) {
System.out.print(j);
}
System.out.println();
}
for (int i = 1; i <=5; i++) {
for (int j = 1; j <=i; j++) {
System.out.print(“*”);
}
System.out.println();
}

}
}

• aanchal says:

cn u plz tell me the code for..
1
121
12123

• Archana Hari Narayanan says:

Need code for
*
**
***
****
*****

• Vikram Routray says:

code–
class print_star
{
public static void main(String args[])
{
int i,j;
for(i=1; i<=4; i++)
{
System.out.println();

{for(j=1; j<=i; j++)
System.out.print(“*”);
}
}
}

}

• Jd Diosana says:

public class Loop
{

public static void main(String args [])
{

int ctr=0;
int ctr1=0;
int n=0;
Scanner scan=new Scanner (System.in);
System.out.println (“Enter number:”);
n=scan.nextInt();

for (ctr1=1;ctr1<=n;ctr1++)
{
for (ctr=1;ctr<=ctr1;ctr++)
{
System.out.print (“*”);
}
System.out.println ();
}

}

}

• Archana Hari Narayanan says:

Need Input for
*
**
***
****
*****

• Parth Talaviya says:

i want this output!!!!
*
* *
* * *
* * * *
* * * * *

• Parth Talaviya says:

not this one but it is like

1 *
2 * *
3 * * *

• Sangepu Srivishal Patel says:

public class numberformat1 {

public static void main(String[] args) {

int row, column;
// Total number of rows it will iterate
for (row = 1; row <= 5; row++) {
// total number of columns it will iterate
for (column = 1; column <= row; column++) {
System.out.print(“*” + ” “);
}
System.out.println(“”);
}
} // end method main
} // end class
hjj

/** write a progarm to print following pattern.

*
**
***
****
*****
*/
class PatternDemo2
{
public static void main(String args[])
{

for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
if(j<=i)
{
System.out.printf(“*”);

}
}
System.out.println();
}
}
}

• geethanjali says:

Console.WriteLine(“input a rows”);
For(int i=1;i<=rows;i++)
{
for(int j=1;j<=i;j++)
{
Console.WriteLine(“*”);
}
}

• Krishnan says:

output:
The pyramid is –
1
232
45654
7890987
123454321
67890109876
2345678765432
901234565432109
78901234543210987
6789012345432109876

• Sangepu Srivishal Patel says:

public class numberformat1 {

public static void main(String[] args) {

int row, column;
// Total number of rows it will iterate
for (row = 1; row <= 5; row++) {
// total number of columns it will iterate
for (column = 1; column <= row; column++) {
System.out.print(“*” + ” “);
}
System.out.println(“”);
}
} // end method main
} // end class

• Rahul Ranjan says:

5 4 3 2 *
5 4 3 * 1
5 4 * 2 1
5 * 3 2 1
* 4 3 2 1
code for it in java??

• sairam velaga says:

public class MypattNum {
public static void main(String[] args) {
for (int i = 1; i <=5; i++) {
for (int j = 5; j >=1; j–) {
if(i==j)
{
System.out.print(“*”);
}
else
{
System.out.print(j);
}
}
System.out.println();

}
}

}

• Rahul Ranjan says:

Thanks bro….

• sairam velaga says:

its ok brother….

• Uncle Drew says:

I want this output
*
*
* *
* *
* * *

• sairam velaga says:

*1
*2*2
*3*3*3
*4*4*4*4
*5*5*5*5*5

i want this output

• Sunil Jamod says:

public class Jamod
{
public static void main(String args[])throws Exception
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(“*”+i);
}
System.out.println();
}
}
}

i want mirror of following pattern……

J
JA
JAV
JAVA

• Testpc1 says:

You can try this -:

public static void main(String[] args) {
String s1=”JAVA”;
char[] array=s1.toCharArray();
for(int i=0;i<=array.length-1;i++){

for (int j=0;j<=i;j++){
System.out.print(array[j]);
}
System.out.println(“”);

}
}

• renz ian cahilig says:

i want this output

*
* *
* * *
* * * *

• Richa Ojha says:

class pattern
{
public static void main()
{
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(” * “);
}
System.out.println();
}
}
}

• renz ian cahilig says:

/\
/ \
/ \
/_ _ _ _ \

• Richa Ojha says:

I want the output of:
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1

• Bibek Samanta says:

public static void main(String []args){

int k=6;
for(int j=15;j>0;j–)
{

for (int i=1;i<k;i++) {=”” system.out.print(j–);=”” }=”” j=”j+1;” k=”k-1;” system.out.println();=”” }=”” }=”” }=””>

• Richa Ojha says:

Thanx

• Ajitha says:

i want this output
______1
____2 1 2
__3 2 1 2 3
_4 3 2 1 2 3 4
…… and so on upto N rows

• Srinivasan karuppasamy says:

i want this output
1
2 3
4 5 6
7 8 9 10
7 8 9 10
4 5 6
2 3
1

• harsha says:

what is the code for
1
2 1
1 2 3
4 3 2 1
1 2 3 4 5
9 8 7 6
10 11 12
14 13
15

• harsha says:

1
3 2
4 5 6
10 9 8 7
13 12 11
15 14
16
what is the code for this??

• Khushbu Saraf says:

class Pattern{
public static void main(int n)
{
int i,j,p,k=1;
for(i=1;i<=n;i++){
if(i%2==0){
for(j=1;j<=i;j++){
k++;
}
p=k-1;
for(j=1;j<=i;j++){
System.out.print(p+”\t”);
p–;
}
}
else
{
for(j=1;j<=i;j++){
System.out.print(k+”\t”);
k++;
}
}
System.out.println();
}

for(i=n-1;i>=1;i–){
if(i%2==0){
for(j=1;j<=i;j++){
k++;
}
p=k-1;
for(j=1;j<=i;j++){
System.out.print(p+”\t”);
p–;
}
}
else
{
for(j=1;j<=i;j++){
System.out.print(k+”\t”);
k++;
}
}
System.out.println();
}
}
}

• Nikhil says:

what is the code for this..
1
3 4
6 7 8
10 11 12 13

• Ashish Bilthare says:

import java.util.*;
class SamplePattern
{
public static void main(String[] arg)
{
int n,m=0,p,j,k;
System.out.println(“enter no.”);
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
n=(n/2)-1;
for(int i=1;i<=n;i++)
{

if(i<=((n/2)+1))
{
m=m+i;
p=m;
for(j=1;j<=i;j++)
{
System.out.print(p+” “);
p–;
}//for j
}//if
else
{
k=n+1;
m=m+(k-i);
p=m;
for(j=1;j<=k-i;j++)
{
System.out.print(p+” “);
p–;
}//for j
}//else
System.out.println();
}//for i
}//main

}//class

• Gulshan Thakur says:

what is the code ?
*
* *
* * *
* * * *
* * *
* *
*

• logaa kirishnamurti says:

please can anyone explain the 12345 pyramid logic of the program

• Asenthus Nyctophilia says:

enter number of raws: 5
””””’1
””””212
”””32123
”’4321234
543212345

note: the ( ‘ ) are space, space didnt show in the message

• Gaurav Dang says:

1
1a2
1a2a3
1a2a3a4
what is the code for this?

• Akshaya Singh says:

for(int i=1; i<= 4 ;i++){

for(int j=1; j <= i; j++){

if(i!=j)
System.out.print(j+”a”);
else
System.out.print(j);
}

System.out.println(“”);
}

• Harshpreet Singh says:

shouldn’t it print like that
12345
2345
345
45
5
??

• Swapnarani Behera says:

Add the alphabet in the given String in Java
Eg: String str = “ABCDEF”

Output : AOBOCODOEOF

• Yato Forever says:

What is the code for
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
I need the answer as soon as possible
Thankyou

• Sivacharan Jakkam says:

what is the code for the given below program?
5
368
6291
58342

and the total must be 5+8+9+8=30.

• himabindu says:

Hi,
I need code for
+
+++
+++++
++++++++

• Tinu Mathai says:

public class patternPlus {

public static void main(String[] args) {
int i, j;
for (i = 1; i <= 4; i++) {
for (j = 1; j < (i * 2); j++) {
System.out.print("+");

}
System.out.println(" ");
}
}
}

• HimaBindu says:

Hi,
How to write a program for frequency(sot by frequency,not by numeric value)
input:
60
65
50
65
70
70
65
60
65
70
output:
65 4
70 3
60 2
50 1

• HimaBindu says:

Hi,
reads a date in yyyy-mm-dd and prints it in words(use appropriate day suffix “st” ,”nd”, “rd”,”th”)
input:

2017-03-04
output:
4th March 2017

• HimaBindu says:

Hi,
print number of character,words and lines.
input:
essay.txt
output:
number of character:1365
number of words:93
nuber of lines:44

• HimaBindu says:

Hi,

• Calvin Manalansan says:

How do you do this?

Create a Java class that takes 1 number as input, then programmatically outputs an inverted triangle, with the input as the starting number.

• Apoorv jain says:

I need a code …
1 2 3 4 5
10 9 8 7 6
11 12 13 14 15