Home > Mobile >  180 degree rotated half pyramid(pattern question in JAVA)
180 degree rotated half pyramid(pattern question in JAVA)

Time:04-19

I am actually trying to print this pattern: https://imgur.com/a/ObixO5I

import java.util.*;

public class ques5 {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            int n = 4;
            // outer loop
            for (int i = 1; i <= n; i  ) {
                // inner loop
                for (int j = 1; j <= n - i; j  ) {
                    System.out.print(" ");
                }
                for (int k = 1; k <= i; k  ) {
                    System.out.print("*");
                }
            }
        }
    }

}

Output I am getting is : * ** *******

CodePudding user response:

Add System.out.println() at end of second inner loop.

CodePudding user response:

Try this

import java.util.*;

public class ques5 {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            int n = 4,m=1;
            // outer loop
            for (int i = n; i >=1; i--) {
                // inner loop
                for (int j = 1; j <= i - 1; j  ) {
                    System.out.print(" ");
                }
                for (int k = 1; k <= m; k  ) {
                    System.out.print("*");
                }
                System.out.print("\n");
                m  ;
            }
        }
    }

}
  •  Tags:  
  • java
  • Related