# Recursion assignment

• 03-16-2007, 07:48 AM
MindGAckt
Recursion assignment
I have an assignment where i have to use a recursion method to print a pattern of asterisks.

Quote:

Write a program, called RecursiveAsterisks, that has a recursive method called printAsterisk that takes an integer n as a parameter. The method should print a pattern of 2n lines of asterisks. The first line contains one asterisk, the next line contains 2, and so on up to the nth line which contains n asterisks. Line number n+1 again contains n asterisks, the next line has n-1 asterisks, and so on until line number 2n which has just one asterisk. Your main method should ask the user for the value of n. Below is an example of what would be displayed by the call printAsterisk(4).

*
**
***
****
****
***
**
How would i make it so that what ever n is currently equal to it prints that many asterisks?

Im pretty lost with recursion so..
• 03-16-2007, 08:58 AM
Kinda Electroni
recursion is:
Recursion is to call yourself over and over.
In your case you have to println(*);
for int i = 0 til i = n; i++
println;
It is not exactly but something like that.
Good luck;
• 03-20-2007, 08:44 AM
MindGAckt
Okay i figured out how to make it print the first part but how do i make it go back?

Code:

```import java.util.Scanner; public class RecursiveAsterisks {         public static void main (String[] args)         {                                 Scanner reader = new Scanner(System.in);                 int n;                 int num;                                 System.out.println("Enter a number: ");                 n = reader.nextInt();                                                 printAsterisks(n);                         }         public static void printAsterisks(int n)         {                 if(n == 0)                         return;                                 if(n > 0) {                         printAsterisks(n - 1);                         for( int i = 0; i < n; i++)                                 System.out.print("*");                         System.out.println();                 }                                         } }```
liek rite now it will only print
*
**
***

But i need it to print
*
**
***
***
**
*
• 03-20-2007, 07:08 PM
masher
try putting the printAsterisks(n-1) after your for loop.
• 03-21-2007, 03:00 PM
Kinda Electroni
Code:

``` /**  * Write a description of class aaaa here.  *  * @author (your name)  * @version (a version number or a date)  */ import java.util.Scanner; public class RecursiveAsterisks {         public static void main (String[] args)         {                                 Scanner reader = new Scanner(System.in);                 int n;                                 System.out.println("Enter a number: ");                 n = reader.nextInt();                 for( int i = 0; i < (n+1); i++)                 printAsterisks(i );                                 for( int i = (n); i > 0; i--)                 printAsterisks(i );         }                         public static void printAsterisks(int n)         {                                 if(n > 0) {                                           for( int i = n; i >0; i--)                                                         System.out.print("*");                         System.out.println();                 }                                         } }```
• 03-21-2007, 07:54 PM
masher

It's not recursive. The first program you posted is recursive.

A recursive method is one that calls itself. If you just call your printAsterisks(n) method, then it will print a single line of asterisks.

I've tried writing some code to solve this, and I can get asterisks like:
Code:

```* ** ***```
and
Code:

```*** ** *```
but I haven't been able to get the double one specified in a single method.

I can fudge it by writing one method that does the increasing one, a second method that does a decreasing one and a third method that calls the first then second. It works, but I think it's outside the intent of the question.

Hint:

This is yours:
Code:

```if(n > 0) {   printAsterisks(n - 1);   for( int i = 0; i < n; i++)       System.out.print("*");   System.out.println(); }```
have a look at this
Code:

```if(n > 0) {   for( int i = 0; i < n; i++)       System.out.print("*");   System.out.println();   printAsterisks(n - 1); }```
• 02-13-2014, 01:54 PM
charonodaemon
Quote:

Originally Posted by masher

It's not recursive. The first program you posted is recursive.

A recursive method is one that calls itself. If you just call your printAsterisks(n) method, then it will print a single line of asterisks.

I've tried writing some code to solve this, and I can get asterisks like:
Code:

```* ** ***```
and
Code:

```*** ** *```
but I haven't been able to get the double one specified in a single method.

I can fudge it by writing one method that does the increasing one, a second method that does a decreasing one and a third method that calls the first then second. It works, but I think it's outside the intent of the question.

Hint:

This is yours:
Code:

```if(n > 0) {   printAsterisks(n - 1);   for( int i = 0; i < n; i++)       System.out.print("*");   System.out.println(); }```
have a look at this
Code:

```if(n > 0) {   for( int i = 0; i < n; i++)       System.out.print("*");   System.out.println();   printAsterisks(n - 1); }```

I have had a look at these and still scratching my head as to how to combine them into one method. Any help on this would be greatly appreciated.