Write a Java program to print same numbers in rectangle rows and columns pattern using for loop.
import java.util.Scanner; public class sameNuminRowsCols1 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Numbers of Rows = "); int rows = sc.nextInt(); System.out.print("Enter Numbers of Columns = "); int columns = sc.nextInt(); System.out.println("Printing Same Numbers in Rows and Columns"); for (int i = 1 ; i <= rows; i++ ) { for (int j = i ; j < i + columns; j++ ) { System.out.printf("%d", j); } System.out.println(); } } }
This Java example displays the rectangle pattern where rows and columns have the same numbers using a while loop.
import java.util.Scanner; public class sameNuminRowsCols2 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int i = 1, j; System.out.print("Enter Numbers of Rows and Columns = "); int rows = sc.nextInt(); int columns = sc.nextInt(); System.out.println("Printing Same Numbers in Rows and Columns"); while(i <= rows ) { j = i ; while ( j < i + columns ) { System.out.printf("%d", j); j++; } System.out.println(); i++; } } }
Enter Numbers of Rows and Columns = 4 4
Printing Same Numbers in Rows and Columns
1234
2345
3456
4567
Java program to print the same numbers in rectangle rows and columns using do while loop.
import java.util.Scanner; public class sameNuminRowsCols3 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Numbers of Rows and Columns = "); int rows = sc.nextInt(); int columns = sc.nextInt(); System.out.println("Printing Same Numbers in Rows and Columns"); int i = 1, j; do { j = i ; do { System.out.printf("%d", j); } while ( ++j < i + columns ); System.out.println(); } while(++i <= rows ); } }
Enter Numbers of Rows and Columns = 5 6
Printing Same Numbers in Rows and Columns
123456
234567
345678
456789
5678910