Write a Java program to display a specified pattern. Within your program you mus
ID: 3678563 • 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
import java.util.Scanner;
public class Pattren
{
public static void Pattern1(int n, int m)
{
if (n == 0)
System.out.println();
else
{
Pattern1 (n-1, m);
for(int a=1; a<=n; a++)
System.out.print(m);
System.out.println();
}
}
public static void Pattern2(int n)
{
if (n == 0)
System.out.println();
else
{
for(int a=0; a<n; a++)
System.out.print(n);
System.out.println();
Pattern2 (n-1);
}
}
public static void main(String[] args)
{
Scanner input = new Scanner (System.in);
System.out.print("Please enter first Integer: ");
int n=input.nextInt();
System.out.print("Please enter second Integer: ");
int m=input.nextInt();
Pattern1(n);
System.out.println();
Pattern2(n);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.