Print Hollow Square with Increasing Number Pattern
In the previous article, we have discussed Java Program to Print Hollow Square with Repeated Number Pattern
In this program we are going to see how to print the hollow square with increasing number pattern.
- Java Code to Print Hollow Square with Increasing Number Pattern
- C Code to Print Hollow Square with Increasing Number Pattern
- C++ Code to Print Hollow Square with Increasing Number Pattern
Example-1 When size value=5 and start number = 1 1 1 1 1 1 2 2 3 3 4 4 5 5 5 5 5
Example-2: When size value=3 and start number = 5 5 5 5 6 6 7 7 7
Now, let’s see the actual program to print it.
Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.
Approach:
- Enter total size and start number store them in integer variables
size
&num
respectively. - Take one outer for loop to iterate the rows,
- Take one inner for loop to iterate the columns.
- After each iteration print a new line.
Java Code to Print Hollow Square with Increasing Number Pattern
import java.util.Scanner; class Main { public static void main(String[] args) { //Create a new Scanner object Scanner scan = new Scanner(System.in); //Taking size as input from user System.out.print("Size of square : "); int size = scan.nextInt(); //Taking number as input from user System.out.print("Number to print from : "); int num = scan.nextInt(); //Row and column are the iterators int numberOfRows, numberOfColumns; //Outer loop to iterate the rows //Iterates from 1 to the size entered by the user for (numberOfRows = 1; numberOfRows <= size; numberOfRows++) { //Inner loop to iterate the columns //Iterates from 1 to the size entered by the user for (numberOfColumns = 1; numberOfColumns <= size; numberOfColumns++) { //Prints the num value if condition matches else prints space if (numberOfColumns == 1 || numberOfColumns == size || numberOfRows == 1 || numberOfRows == size) System.out.print(num+ " "); else System.out.print(" "); } //Incrementing numbe after each iteration num++; //Prints a newline System.out.println(); } } }
Output: Size of square : 5 Number to print from : 1 1 1 1 1 1 2 2 3 3 4 4 5 5 5 5 5
C Code to Print Hollow Square with Increasing Number Pattern
#include <stdio.h> int main() { //Taking size as input from user printf("Size of square : "); int size; scanf("%d", &size); //Taking number as input from user printf("Number to print from : "); int num; scanf("%d", &num); //Row and column are the iterators int numberOfRows, numberOfColumns; //Outer loop to iterate the rows //Iterates from 1 to the size entered by the user for (numberOfRows = 1; numberOfRows <= size; numberOfRows++) { //Inner loop to iterate the columns //Iterates from 1 to the size entered by the user for (numberOfColumns = 1; numberOfColumns <= size; numberOfColumns++) { //Prints the num value if condition matches else prints space if (numberOfColumns == 1 || numberOfColumns == size || numberOfRows == 1 || numberOfRows == size) printf("%d ", num); else printf(" "); } //Incrementing numbe after each iteration num++; //Prints a newline printf("\n"); } return 0; }
Output: Size of square : 5 Number to print from : 1 1 1 1 1 1 2 2 3 3 4 4 5 5 5 5 5
C++ Code to Print Hollow Square with Increasing Number Pattern
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { //Taking size as input from user printf("Size of square : "); int size; cin >> size; //Taking number as input from user printf("Number to print from : "); int num; cin >> num; //Row and column are the iterators int numberOfRows, numberOfColumns; //Outer loop to iterate the rows //Iterates from 1 to the size entered by the user for (numberOfRows = 1; numberOfRows <= size; numberOfRows++) { //Inner loop to iterate the columns //Iterates from 0 to one less than the size entered by the user for (numberOfColumns = 1; numberOfColumns <= size; numberOfColumns++) { //Prints the num value if condition matches else prints space if (numberOfColumns == 1 || numberOfColumns == size || numberOfRows == 1 || numberOfRows == size) cout << num << " "; else cout << " "; } //Incrementing numbe after each iteration num++; //Prints a newline cout << endl; } return 0; }
Output: Size of square : 5 Number to print from : 1 1 1 1 1 1 2 2 3 3 4 4 5 5 5 5 5
Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.
Related Java Number Pattern Programs: