# Hourglass using recursion

Page 1 of 1

## 2 Replies - 11829 Views - Last Post: 30 April 2012 - 04:50 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=277435&amp;s=8de9f2f600d606f34879cf5cea2f907a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 nweid1

Reputation: 0
• Posts: 14
• Joined: 12-February 12

# Hourglass using recursion

Posted 29 April 2012 - 10:59 PM

I have to write a recursive program that makes an hourglass that suites the size of the user's input. For example, if the user types 4, this should print:
```* * * *
* * *
* *
*
* *
* * *
* * * *
```

So far I am able to print the stars but I'm not sure how to deal with the spaces. My teacher said to use a count and I added it but I'm not sure how to get use it appropriately. Here is an example of what prints
```* * * *
* * *
* *
*
* *
* * *
* * * *
```

and here is my code

```import java.util.InputMismatchException;
import java.util.Scanner;

public class Hourglass {

public static void main(String[] args) {

Scanner console = new Scanner(System.in);
System.out.println("Enter an integer greater than or equal to 1");
int n = 0;
try {
n = console.nextInt();
} catch (InputMismatchException e) {
e.getMessage();
}
if (n < 1) {
System.out.println("Invalid input");
} else {
System.out.println("");
hourglass(n, 0);
System.out.println("");
}

}

public static void hourglass(int n, int count) {
recursion(n);
if(n > 1) {
spaces(n, count);
hourglass(n-1, count);
recursion(n);
}
}

public static void recursion(int n) {
System.out.print("* ");
if (n > 1)
recursion(n - 1);
else
System.out.println();
}

public static void spaces(int count, int n) {
if(count<n) {
spaces(count-1, n);
}
System.out.print(" ");
}
}

```

Thanks!

This post has been edited by baavgai: 30 April 2012 - 04:27 AM
Reason for edit:: tagged stars, because I couldn't tell the diff

Is This A Good Question/Topic? 0

## Replies To: Hourglass using recursion

### #2 collegekid901

Reputation: 8
• Posts: 41
• Joined: 09-March 12

## Re: Hourglass using recursion

Posted 29 April 2012 - 11:39 PM

Easy solution is to every time you lose a star you add a space and every time you gain a star you lose a space. So for line 1, 0 spaces. line 2, 1 space. line 3 , 2 spaces etc

### #3 baavgai

• Dreaming Coder

Reputation: 7118
• Posts: 14,835
• Joined: 16-October 07

## Re: Hourglass using recursion

Posted 30 April 2012 - 04:50 AM

Your code will give cleaner output, if still wrong, if you did this:
```public static void main(String[] args) {
hourglass(4, 0);
}

public static void hourglass(int n, int count) {
if(n > 1) {
recursion(n);
hourglass(n-1, count);
}
recursion(n);
}

```

Note, you never use count? Indeed, you may not for the output you're currently getting. I bet it would be useful for each line...

Maybe:
```public static void hourglass(int n) {
hourglass(n, n);
}

public static void hourglass(int n, final int count) {
if(n > 1) {
recursion(n, count);
hourglass(n-1, count);
}
recursion(n, count);
}

```

If you want to keep the recursion as sloppy loop thing going; make it generic:
```public static void dup(int n, String s) {
if (n > 0) {
System.out.print(s);
dup(n-1, s);
}
}

```

You'll want to call that twice, once for spaces with a count modifier. Once for stars. And then add a new line.

Hope this helps.