You are here : Home / Core Java Tutorials / Interview Programs (beginner to advanced) in java / Pattern/Pyramid generating programs in java
Contents of page >
- Pattern/ Pyramid 1 in java
- Pattern/ Pyramid 2 in java
- Pattern/ Pyramid 3 in java
- Pattern/ Pyramid 4 in java
- Pattern/ Pyramid 5 in java
- Pattern/ Pyramid 6 in java
- Pattern/ Pyramid 7 in java
- Pattern/ Pyramid 8 in java
- Pattern/ Pyramid 9 in java
- Pattern/ Pyramid 10 in java
- Pattern/ Pyramid 11 in java
- Pattern/ Pyramid 12 in java
- Pattern/ Pyramid 13 in java
- Pattern/ Pyramid 14 in java
Pattern/ Pyramid 1 in java
Write a program to generate this Pattern/pyramid in java.
Example / Full Program/SourceCode to generate Pattern/ Pyramid in java >
package pyramid;
import java.util.Scanner;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class Pyramid1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter n : ");
int n = scanner.nextInt();
System.out.println("");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println("");
}
}
}
/*OUTPUT
Enter n : 5
1
12
123
1234
12345
*/
|
Pattern/ Pyramid 2 in java
Write a program to generate this Pattern in java.
Example / Full Program/SourceCode to generate Pattern/ Pyramid in java >
package pyramid;
import java.util.Scanner;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class Pyramid2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter n : ");
int n = scanner.nextInt();
System.out.println("");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
System.out.println("");
}
}
}
/*OUTPUT
Enter n : 5
1
22
333
4444
55555
*/
|
Pattern/ Pyramid 3 in java
Write a program to generate this Pattern in java.
Example / Full Program/SourceCode to generate Pattern/ Pyramid in java >
package pyramid;
import java.util.Scanner;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class Pyramid3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter n : ");
int n = scanner.nextInt();
System.out.println("");
for (int i = n; i >0; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println("");
}
}
}
/*OUTPUT
Enter n : 5
12345
1234
123
12
1
*/
|
Pattern/ Pyramid 4 in java
Write a program to generate this Pattern/pyramid in java.
Example / Full Program/SourceCode to generate Pattern/ Pyramid in java >
package pyramid;
import java.util.Scanner;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class Pyramid4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter n : ");
int n = scanner.nextInt();
System.out.println("");
for (int i = n; i >0; i--) {
for (int j = n,k=1; k <= i; j++,k++) {
System.out.print(j);
}
System.out.println("");
}
}
}
/*OUTPUT
Enter n : 5
56789
5678
567
56
5
*/
|
Pattern/ Pyramid 5 in java
Write a program to generate this Pattern in java.
Example / Full Program/SourceCode to generate Pattern/ Pyramid in java >
package pyramid;
import java.util.Scanner;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class Pyramid5 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter n : ");
int n = scanner.nextInt();
System.out.println("");
for (int i = n; i >0; i--) {
for (int j = n,k=1; k <= i; j--,k++) {
System.out.print(j);
}
System.out.println("");
}
}
}
/*OUTPUT
Enter n : 5
54321
5432
543
54
5
*/
|
Pattern/ Pyramid 6 in java
Write a program to generate this Pattern/pyramid in java.
Example / Full Program/SourceCode to generate Pattern/ Pyramid in java >
package pyramid;
import java.util.Scanner;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class Pyramid6 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter n : ");
int n = scanner.nextInt();
System.out.println("");
for (int i = n; i >0; i--) {
for (int j = i,k=1; k <= i; j--,k++) {
System.out.print(j);
}
System.out.println("");
}
}
}
/*OUTPUT
Enter n : 5
54321
4321
321
21
1
*/
|
Pattern/ Pyramid 7 in java
Write a program to generate this Pattern in java.
Example / Full Program/SourceCode to generate Pattern/ Pyramid in java >
package pyramid;
import java.util.Scanner;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class Pyramid7Floyds {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter rows in Floyd's triangle : ");
int rows = scanner.nextInt();
System.out.println("");
int nextNumber = 1;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(nextNumber<10 ? (" " + nextNumber++) : (" " + nextNumber++) ); //2spaces in single digit & 1 space in double digit.
//System.out.format("%3d",nextNumber++ ); //You may use this line for formatting as a replacement of above line. (comment above line before using this)
}
System.out.println();
}
}
}
/*OUTPUT
Enter rows in Floyd's triangle : 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
*/
|
Pattern/ Pyramid 8 in java
Write a program to generate this Pascal’s triangle in java.
>Each number present in row is sum of the left & right number in the above row.
>If there is no number in number in above, number is assumed as 0 in java.
>First and last number in row is always 1.
>Sum of numbers in each row is equal to sum of numbers in above row.
Example / Full Program/SourceCode to generate Pattern/ Pyramid in java >
package pyramid;
/*
*/
import java.util.Scanner;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class Pyramid8Pascal {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter no of rows in Pascal's triangle : ");
int rows = scanner.nextInt();
int nextNumber;
System.out.println("");
for (int i = 0; i < rows; i++) {
nextNumber = 1;
for(int k=0; k<(rows-i)*2; k++) //create (rows-i)*2 spaces, for initial spacing.
System.out.print(" ");
for (int j = 0; j <= i; j++) {
System.out.format("%4d", nextNumber); // %4d creates 4 space between number.
nextNumber = nextNumber * (i - j) / (j + 1);
}
System.out.println();
}
}
}
/* OUTPUT
Enter no of rows in Pascal's triangle : 6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
*/
|
Pattern/ Pyramid 9 in java
Write a program to generate this Pattern/pyramid in java.
Example / Full Program/SourceCode to generate Pattern/ Pyramid in java >
package pyramid;
import java.util.Scanner;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class Pyramid9Double {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter n : ");
int n = scanner.nextInt();
System.out.println("");
for (int i = 1; i <= n; i++) { //it will create upper half
for (int j = 1; j <= i; j++) {
System.out.print(" *");
}
System.out.println("");
}
for (int i = n; i > 0; i--) { //it will create lower half
for (int j = 1; j <=i; j++) {
System.out.print(" *");
}
System.out.println("");
}
}
}
/*OUTPUT
Enter n : 4
*
**
***
****
****
***
**
*
*/
|
Pattern/ Pyramid 10 in java
Write a program to generate this Pattern in java.
Example / Full Program/SourceCode to generate Pattern/ Pyramid in java >
package pyramid;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class Pyramid10 {
public static void main(String[] args) {
int j;
int rows = 8;
for (int i = 1; i <= rows; i++) {
for (j = 1; j <= rows - i; j++) // for initial spacing.
System.out.print(" ");
for (int k = j + 1; k <= rows; k++) //creates left half.
System.out.print("*");
for (int k = rows; k > j - 1; k--)//creates right half.
System.out.print("*");
System.out.println();
}
}
}
/*OUTPUT
*
***
*****
*******
*********
***********
*************
***************
*/
|
Pattern/ Pyramid 11 in java
Write a program to generate this Pattern in java.
Example / Full Program/SourceCode to generate Pattern/ Pyramid in java >
package pyramid;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class Pyramid11 {
public static void main(String[] args) {
int rows = 8;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= (rows - i)*2; j++) //create initial spacing.
System.out.print(" ");
for (int k = i; k >= 1; k--) //creates left half.
System.out.print(" "+ k);
for (int k = 2; k <= i; k++)//creates right half.
System.out.print(" "+ k);
System.out.println();
}
}
}
/*OUTPUT
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
*/
|
Pattern/ Pyramid 12 in java
Write a program to generate this Pattern in java.
Example / Full Program/SourceCode to generate Pattern/ Pyramid in java >
package pyramid;
import java.util.Scanner;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class Pyramid12 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter n : ");
int n = scanner.nextInt();
System.out.println("");
for (int i = 1; i <= n; i++) {
int k=i;
for (int j = 1; j <= n; j++) {
System.out.print((j<= n-i ? " " : k--));
}
System.out.println();
}
}
}
/*OUTPUT
Enter n : 5
1
21
321
4321
54321
*/
|
Pattern/ Pyramid 13 in java
Write a program to generate this Pattern in java.
Example / Full Program/SourceCode to generate Pattern/ Pyramid in java >
package pyramid;
import java.util.Scanner;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class Pyramid13 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter n : ");
int n = scanner.nextInt();
System.out.println("");
for (int i = 1; i <= n; i++) {
int k=i;
for (int j = 1; j <= n; j++) {
if(j<i)
System.out.print(" ");
else
System.out.print(k++);
}
System.out.println("");
}
}
}
/*OUTPUT
Enter n : 5
12345
2345
345
45
5
*/
|
Pattern/ Pyramid 14 in java
Write a program to generate this Pattern in java.
We will be using jagged array to generate such pyramid.
Example / Program to generate above pattern in java >
/** JavaMadeSoEasy.com */
public class Pyramid14 {
public static void main(String... args) {
int ar[][] = new int[5][]; //jagged array
int start = 0;
int prev = 0;
int sum = 1; // keep initial sum as 0
for (int i = 0; i < ar.length; i++) {
ar[i] = new int[i + 1];
prev = 0;
for (int j = 0; j < ar[i].length; j++) {
if (i > 0 && j > 0) {
prev = ar[i][j - 1];
start = ar[i - 1][j - 1];
sum = start + prev;
ar[i][j] = sum;
} else {
ar[i][j] = sum;
}
System.out.print(sum + " ");
}
start = sum; // assign sum to start
System.out.println(); // for new line
} // end for loop
}
}
/*
1
1 2
2 3 5
5 7 10 15
15 20 27 37 52
*/
|
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
RELATED LINKS>
>Pattern/Pyramid generating programs
Labels:
Core Java