Here Is Your Pyramid 9 8 9 8 7 8 9 8 7 6 7 8 9 8 7 6 5 6 7 8 9 8 7 6 5 4 5 6 7 8 9 8 7 6 5 4 3 4 5 6 7 8 9 8 7 6 5 4 3 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
In this problem, at the end of each row we print ‘j’ where ‘j’ value will be from i to noOfRows and from noOfRows-1 to i.
Java program to print reverse pyramid of numbers
- import java.util.Scanner;
- public class MainClass {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- //Taking noOfRows value from the user
- System.out.println("How Many Rows You Want In Your Pyramid?");
- int noOfRows = sc.nextInt();
- //Initializing rowCount with 1
- int rowCount = 1;
- System.out.println("Here Is Your Pyramid");
- //Implementing the logic
- for (int i = noOfRows; i >= 1; i--) {
- //Printing i*2 spaces at the beginning of each row
- for (int j = 1; j <= i*2; j++) {
- System.out.print(" ");
- }
- //Printing j where j value will be from i to noOfRows
- for (int j = i; j <= noOfRows; j++) {
- System.out.print(j+" ");
- }
- //Printing j where j value will be from noOfRows-1 to i
- for (int j = noOfRows-1; j >= i; j--) {
- System.out.print(j+" ");
- }
- System.out.println();
- //Incrementing the rowCount
- rowCount++;
- }
- }
- }
Here Is Your Pyramid 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 6 5 4 3 2 1 1 2 3 4 5 6 5 4 3 2 1 1 2 3 4 5 4 3 2 1 1 2 3 4 3 2 1 1 2 3 2 1 1 2 1 1
In this problem, we iterate outer loop in the reverse order i.e from 1 to noOfRows and initialize the rowCount to noOfRows.
- import java.util.Scanner;
- public class MainClass {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- //Taking noOfRows value from the user
- System.out.println("How Many Rows You Want In Your Pyramid?");
- int noOfRows = sc.nextInt();
- //Initializing rowCount with noOfRows
- int rowCount = noOfRows;
- System.out.println("Here Is Your Pyramid");
- //Implementing the logic
- for (int i = 0; i < noOfRows; i++) {
- //Printing i*2 spaces at the beginning of each row
- for (int j = 1; j <= i*2; j++) {
- System.out.print(" ");
- }
- //Printing j where j value will be from 1 to rowCount
- for (int j = 1; j <= rowCount; j++) {
- System.out.print(j+" ");
- }
- //Printing j where j value will be from rowCount-1 to 1
- for (int j = rowCount-1; j >= 1; j--) {
- System.out.print(j+" ");
- }
- System.out.println();
- //Decrementing the rowCount
- rowCount--;
- }
- }
- }
No comments:
Post a Comment