Write a Java program to display a specified pattern. Within your program you mus
ID: 3680063 • Letter: W
Question
Write a Java program to display a specified pattern. Within your program you must define a recursive method with the following signature. public static void Pattern1(int m, int n). Your program will ask the user to input two integers, the first one is for the number of the rows in the pattern and the second one, which is between 0 and 9 inclusively, is for the entries in the pattern, and your program invokes the method. A sample run of your program is as follows:
Please input the first integer.
3
Please input the second integer.
0
The pattern is as follows.
0
00
000
Explanation / Answer
Hi, Please find below the desired program. Recursive function has been implemented as per the requirement in this program.
public class PatternDisplay {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
java.util.Scanner in = new java.util.Scanner(System.in);
System.out.println("Please Enter Number of Rows in Pattern..");
int rows = in.nextInt();
System.out.println("Please Enter the integer number between 0 and 9 inclusive..");
int number = in.nextInt();
System.out.println(" ");
Pattern1(rows, number);
}
public static void Pattern1(int m, int n){
if(m == 0)
return;
Pattern1(--m, n);
for(int i=1 ; i<= m+1; i++){
System.out.print(n);
}
System.out.print(" ");
}
}
Output:
Please Enter Number of Rows in Pattern..
6
Please Enter the integer number between 0 and 9 inclusive..
9
9
99
999
9999
99999
999999
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.