DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Registered User
Join Date
Oct 2006
Posts
11

## Recursion assignment

I have an assignment where i have to use a recursion method to print a pattern of asterisks.

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..

2. Registered User
Join Date
May 2005
Location
Posts
173

## 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;

3. Registered User
Join Date
Oct 2006
Posts
11
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)
{

int n;
int num;

System.out.println("Enter a number: ");

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
*
**
***
***
**
*

4. Registered User
Join Date
Jul 2005
Posts
78
try putting the printAsterisks(n-1) after your for loop.

5. Registered User
Join Date
May 2005
Location
Posts
173
Code:
```/**
* Write a description of class aaaa here.
*
* @version (a version number or a date)
*/
import java.util.Scanner;

public class RecursiveAsterisks
{
public static void main (String[] args)
{

int n;

System.out.println("Enter a number: ");
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();
}

}
}```

6. Registered User
Join Date
Jul 2005
Posts
78

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);
}```
Last edited by masher; 03-21-2007 at 08:04 PM.

7. Registered User
Join Date
Feb 2014
Posts
1
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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise