Write a Java program to print square of right increment numbers pattern using for loop.
package Shapes3; import java.util.Scanner; public class SquareRightincNum1 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Square of Right Increment Numbers Rows = "); int rows = sc.nextInt(); System.out.println("Square of Increment Numbers from Right Side"); for (int i = 1; i <= rows; i++ ) { for (int j = 1; j <= rows - i; j++ ) { System.out.print(1 + " "); } for(int k = 1; k <= i; k++) { System.out.print(i + " "); } System.out.println(); } } }
This Java program prints the square pattern of increment numbers from right side using a while loop.
package Shapes3; import java.util.Scanner; public class SquareRightincNum2 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Square of Right Increment Numbers Rows = "); int rows = sc.nextInt(); System.out.println("Square of Increment Numbers from Right Side"); int j, k, i = 1; while( i <= rows) { j = 1; while(j <= rows - i ) { System.out.print(1 + " "); j++; } k = 1; while( k <= i) { System.out.print(i + " "); k++; } System.out.println(); i++; } } }
Enter Square of Right Increment Numbers Rows = 9
Square of Increment Numbers from Right Side
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 2 2
1 1 1 1 1 1 3 3 3
1 1 1 1 1 4 4 4 4
1 1 1 1 5 5 5 5 5
1 1 1 6 6 6 6 6 6
1 1 7 7 7 7 7 7 7
1 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9
In this Java example, we created a squareIncNum function that will display the square pattern of increment numbers from the right hand side.
package Shapes3; import java.util.Scanner; public class SquareRightincNum3 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Square of Right Increment Numbers Rows = "); int rows = sc.nextInt(); System.out.println("Square of Increment Numbers from Right Side"); squareIncNum(rows); } public static void squareIncNum(int rows) { for (int i = 1; i <= rows; i++ ) { for (int j = 1; j <= rows - i; j++ ) { System.out.print(1 + " "); } for(int k = 1; k <= i; k++) { System.out.print(i + " "); } System.out.println(); } } }
Enter Square of Right Increment Numbers Rows = 5
Square of Increment Numbers from Right Side
1 1 1 1 1
1 1 1 2 2
1 1 3 3 3
1 4 4 4 4
5 5 5 5 5