This code is implemented and provided by AJIT, from GNIOT College, Gretaer Noida
Objective : print a star(*) diamond using recursion
Algorithm:
step 1: get the term(A natural number) to print the diamond
step 2: for spaces take a space counter(a variable), for line take
a line counter, for revolution take a revolution counter.
step 3: to use recursion we need a base condition to stop it.
For that we need to count total number of characters to be printed
(including spaces also).
For Example..
if the value of term is 5. then total number of characters will be
45 (include a space with star).
it means we can calculate it by using [5 * (2 * 5 - 1)] = 45.
step 4: we use a constructor to initialize the value of the term (also generate
a copy of it term for the further requirements)
step 5: now we need to find the condition on which we have to take a new line.
For Example...
if term = 5;
then in the first line we print 4 spaces and 1 star(total 5),
in second line 3 spaces and 2 stars(total = 5).
It means we see that total number of characters in each line is 5.
Now we can say that if the revolutionCounter module becomes zero.
It will be the condition for the next line.
step 6: Now in every next line we need to modify the the condition controller
variable of spaces.
In upper part of the diamond we need to decrease the spaces and after
that we have to increase it.
For Example...
if term = 5, after the 5th line we need to increase the spaces.
We need to make a condition to control it on the basis of line counter.
step 7: Now we have to call the method recursively. We need to use
revolution counter and the total number of revolution(variables) to
control the recursion.
By using this algorithm we can print a star diamond.
Special Note by AJIT: I tried my level best in to design this algorithm.
Please post your valuable comments for morerefinements.
Note: Please save this class inside DiamondShapeByUsingRecursion.java file in Eclipse IDE
import java.util.Scanner;
public class DiamondShapeByUsingRecursion
{
// declare some non-static variables (bole toh instance variables)
int term;
int copyOfTerm;
int totalRevolution;
int counterPerRevolution = 0;
int forSpaces = 1;
int lineCounter = 1;
public DiamondShapeByUsingRecursion(int term)
{
this.term = term;
totalRevolution = (2 * this.term - 1) * this.term;
this.copyOfTerm = this.term;
}
public void diamond()
{
if (forSpaces < copyOfTerm)
{
System.out.print(" ");
forSpaces++;
counterPerRevolution++;
}
else
{
System.out.print(" *");
counterPerRevolution++;
}
if (counterPerRevolution % (term) == 0)
{
System.out.println();
forSpaces = 1;
if (lineCounter < term)
{
copyOfTerm--;
}
else
{
copyOfTerm++;
}
lineCounter++;
}
if (counterPerRevolution < totalRevolution)
{
diamond();
}
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter the no of terms for pyramid: ");
int terms = s.nextInt();
// create object of class and pass the value of terms inside the constructor
DiamondShapeByUsingRecursion b =
new DiamondShapeByUsingRecursion(terms);
System.out.println("Printing diamond of size "+term);
// invoke the method in order to print pyramid
b.diamond();
} // end of main method
} // end of class
Objective : print a star(*) diamond using recursion
Algorithm:
step 1: get the term(A natural number) to print the diamond
step 2: for spaces take a space counter(a variable), for line take
a line counter, for revolution take a revolution counter.
step 3: to use recursion we need a base condition to stop it.
For that we need to count total number of characters to be printed
(including spaces also).
For Example..
if the value of term is 5. then total number of characters will be
45 (include a space with star).
it means we can calculate it by using [5 * (2 * 5 - 1)] = 45.
step 4: we use a constructor to initialize the value of the term (also generate
a copy of it term for the further requirements)
step 5: now we need to find the condition on which we have to take a new line.
For Example...
if term = 5;
then in the first line we print 4 spaces and 1 star(total 5),
in second line 3 spaces and 2 stars(total = 5).
It means we see that total number of characters in each line is 5.
Now we can say that if the revolutionCounter module becomes zero.
It will be the condition for the next line.
step 6: Now in every next line we need to modify the the condition controller
variable of spaces.
In upper part of the diamond we need to decrease the spaces and after
that we have to increase it.
For Example...
if term = 5, after the 5th line we need to increase the spaces.
We need to make a condition to control it on the basis of line counter.
step 7: Now we have to call the method recursively. We need to use
revolution counter and the total number of revolution(variables) to
control the recursion.
By using this algorithm we can print a star diamond.
Special Note by AJIT: I tried my level best in to design this algorithm.
Please post your valuable comments for morerefinements.
Note: Please save this class inside DiamondShapeByUsingRecursion.java file in Eclipse IDE
import java.util.Scanner;
public class DiamondShapeByUsingRecursion
{
// declare some non-static variables (bole toh instance variables)
int term;
int copyOfTerm;
int totalRevolution;
int counterPerRevolution = 0;
int forSpaces = 1;
int lineCounter = 1;
public DiamondShapeByUsingRecursion(int term)
{
this.term = term;
totalRevolution = (2 * this.term - 1) * this.term;
this.copyOfTerm = this.term;
}
public void diamond()
{
if (forSpaces < copyOfTerm)
{
System.out.print(" ");
forSpaces++;
counterPerRevolution++;
}
else
{
System.out.print(" *");
counterPerRevolution++;
}
if (counterPerRevolution % (term) == 0)
{
System.out.println();
forSpaces = 1;
if (lineCounter < term)
{
copyOfTerm--;
}
else
{
copyOfTerm++;
}
lineCounter++;
}
if (counterPerRevolution < totalRevolution)
{
diamond();
}
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter the no of terms for pyramid: ");
int terms = s.nextInt();
// create object of class and pass the value of terms inside the constructor
DiamondShapeByUsingRecursion b =
new DiamondShapeByUsingRecursion(terms);
System.out.println("Printing diamond of size "+term);
// invoke the method in order to print pyramid
b.diamond();
} // end of main method
} // end of class