using recursion write a java code which outputs the stars inthe following patter
ID: 3619391 • Letter: U
Question
using recursion write a java code which outputs the stars inthe following pattern but it should ask the user to input anonnegative integer for stars & the number oflinesoutput should look like this : for eg if the input for stars is 3 & lines is 5then output as follows: * *** * * * ** ** * * * * ** * * * ** ** * * ** * And if for eg if the input for stars is 3 &lines is 4 then output as follows: * *** * * * ** ** * * * * ** * * * ** ** i want all the three output when the input is made.... using recursion write a java code which outputs the stars inthe following pattern but it should ask the user to input anonnegative integer for stars & the number oflines
output should look like this : for eg if the input for stars is 3 & lines is 5then output as follows: * *** * * * ** ** * * * * ** * * * ** ** * * ** * And if for eg if the input for stars is 3 &lines is 4 then output as follows: * *** * * * ** ** * * * * ** * * * ** ** * *** * * * ** ** * * * * ** * * * ** ** * *** * * * ** ** * * * * ** * * * ** ** i want all the three output when the input is made....
Explanation / Answer
please rate - thanks import java.util.*; public class recursiveshapes{ public static void main(String[]args)throws Exception { Scanner in = new Scanner(System.in); int i,s; System.out.print("How many rows? "); i=in.nextInt(); System.out.print("How many stars? "); s=in.nextInt(); line(s,1,i); System.out.println(" "); bump(1,s,1,i,s); System.out.println(" "); tri(1,s,1,i,s); } public static void line(int m,int n,int p) {int i; //System.out.println(m+" "+n+" "+p); if(p==0) return; for(i=0;i0) System.out.println(); if(m==1||n==0) line(m+1,0,p-1); else line(m-1,n,p-1); } public static void bump(int r,int m,int n,int p,int s) {int i; //System.out.println(r+" "+m+" "+p); if(p==0) return; for(i=0;i0) System.out.println(); if(r>=s||n==0) bump(r-1,m+1,0,p-1,s); else bump(r+1,m-1,1,p-1,s); } public static void tri(int r,int m,int n,int p,int s) {int i; //System.out.println(r+" "+m+" "+n+" "+p); if(p==0) return; for(i=0;i=s||n==0) tri(r-1,m+1,0,p-1,s); else tri(r+1,m-1,1,p-1,s); } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.