Home how to mirror an hourglass with no more than four for loops?
Reply: 0

how to mirror an hourglass with no more than four for loops?

user948
1#
user948 Published in April 25, 2018, 12:36 am

I would like to find out how to mirror one half hourglass... with not more than 4 loops. I do not want to use recursion or arrays, just simple for loops

    Scanner sc = new Scanner(System.in);
    System.out.println(" Enter odd number above 0 here: ");
    int h = sc.nextInt();
    char x = '#';

    if (h % 2 != 0) {
        for (int i = 1; i <= h; i++) {

            // loop for first part of hourglass
            for (int j = 1; j <= i; j++) {
                System.out.print(x);
            }
            // create white space
            for (int j = h - i; j >= 1; j--) {
                System.out.print("  ");
            }
            // create mirror
            for (int k = i; k >= 1; k--) {
                System.out.print(x);
            }

            System.out.println();
        }

    } else {
        System.out.println(" Not an odd number. Try again: ");
    }
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.383965 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO