In: Computer Science
JAVA
Write nested while loop that will print out this pattern, based upon a number entered by the user. User enters 4: 1234 1234 1234 1234 User enters 2: 12 12
import java.util.*;
public class Pattern {
public static void main(String[] args) {
try {
int value;
Scanner scan=new
Scanner(System.in);
//Taking user input
System.out.print("Enter a number: ");
value=scan.nextInt();
if(value<=0)
System.out.println("Please enter value greater than
zero");
else {
int i=1,j=1;
//Looping to print the values
while(i<=value) {
j=1;
while(j<=value) {
//If you want space between
number,use below line
//System.out.print(j+" ");
//If you do not want space between
number, use below line
System.out.print(j);
j=j+1;
}
System.out.print("\n");
i=i+1;
}
}
}
catch(Exception e) {
System.out.println("Enter only numbers");
}
}
}
Sample input and output:
Enter a number: 4
1234
1234
1234
1234
Enter a number: 2
12
12
//With spaces between numbers
Enter a number: 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
Enter a number: 2
1 2
1 2