Must be done in java Consider a frame of bowling pins shown below, where each *
ID: 3730325 • Letter: M
Question
Must be done in java Consider a frame of bowling pins shown below, where each * represents a pin: * * * * * * * * * * * * * * *There are 5 rows and a total of 15 pins.
If we had only the top 4 rows, then there would be a total of 10 pins.
If we had only the top three rows, then there would be a total of six pins.
If we had only the top two rows, then there would be a total of three pins.
If we had only the top row, then there would be a total of one pin.
Write a program that takes as input the number of rows n and outputs the total number of pins that would exist in a pyramid with n rows.
NOTE: Your implementation must include the definition/implementation and use/calling of a recursive method numberOfPins that is passed the number of rows to its parameter variable, and returns the number of pins.
——- Use the Design Recipe to design the algorithm for each method (main and others), showing all work as comments in the source code (.java) file. Using Eclipse, implement the algorithm as a program. Must be done in java Consider a frame of bowling pins shown below, where each * represents a pin: * * * * * * * * * * * * * * *
There are 5 rows and a total of 15 pins.
If we had only the top 4 rows, then there would be a total of 10 pins.
If we had only the top three rows, then there would be a total of six pins.
If we had only the top two rows, then there would be a total of three pins.
If we had only the top row, then there would be a total of one pin.
Write a program that takes as input the number of rows n and outputs the total number of pins that would exist in a pyramid with n rows.
NOTE: Your implementation must include the definition/implementation and use/calling of a recursive method numberOfPins that is passed the number of rows to its parameter variable, and returns the number of pins.
——- Use the Design Recipe to design the algorithm for each method (main and others), showing all work as comments in the source code (.java) file. Using Eclipse, implement the algorithm as a program. Must be done in java Consider a frame of bowling pins shown below, where each * represents a pin: * * * * * * * * * * * * * * *
There are 5 rows and a total of 15 pins.
If we had only the top 4 rows, then there would be a total of 10 pins.
If we had only the top three rows, then there would be a total of six pins.
If we had only the top two rows, then there would be a total of three pins.
If we had only the top row, then there would be a total of one pin.
Write a program that takes as input the number of rows n and outputs the total number of pins that would exist in a pyramid with n rows.
NOTE: Your implementation must include the definition/implementation and use/calling of a recursive method numberOfPins that is passed the number of rows to its parameter variable, and returns the number of pins.
——- Use the Design Recipe to design the algorithm for each method (main and others), showing all work as comments in the source code (.java) file. Using Eclipse, implement the algorithm as a program. Use the Design Recipe to design the algorithm for each method (main and others), showing all work as comments in the source code (.java) file. Using Eclipse, implement the algorithm as a program.
Explanation / Answer
public class pattern {
public static void fun(int n,int j){ //Every time passing the same row number and j is for number of spaces.
int i;
if(n==0)return ;
for(i=0;i<n-j;i++)System.out.print(" "); //number of space in each row are calculated by n-j
for(i=0;i<j;i++)System.out.print("* "); //space after * is for alignment only
System.out.println();
if(j<n)
fun(n,j+1);
}
public static void main(String argv[]){
int n;
Scanner scan=new Scanner(System.in);
n=scan.nextInt();
fun(n,1); // intially we need to print 1st row
// System.out.println("Total number of pins printed are : "+ (n*(n+1))/2);
}
}
// I hope u will appreciate me for this solution . If any quiries comment on this. Thanks in advance....
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.