Print Hollow Numerical Parallelogram
In the previous article, we have discussed Java Program to Print Trapezium Number Pattern
In this article we will see how to print hollow numerical parallelogram.
- Java Code to Print Hollow Numerical Parallelogram
- C Code to Print Hollow Numerical Parallelogram
- C++ Code to Print Hollow Numerical Parallelogram
Example-1 When size value=5 555555555 4444 4444 333 333 22 22 1 1 22 22 333 333 4444 4444 555555555
Example-2: When size value=7 7777777777777 666666 666666 55555 55555 4444 4444 333 333 22 22 1 1 22 22 333 33 4444 4444 55555 55555 666666 666666 7777777777777
Now, let’s see the actual program to print it.
Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.
Approach:
- Enter total size and number store them in integer variables size.
- Take one outer for loop to iterate the rows and print upper half of the rectangle
- Take second outer for printing the lower half of the pattern .
Java Code to Print Hollow Numerical Parallelogram
import java.util.Scanner; class Main { public static void main(String[] args) { // taking size of no. of iteration //Row and column are the iterators int size , numberOfRows , numberOfColumns,k = 0, spaces = 1; // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.print("Enter the number of rows : "); size = scanner.nextInt(); // This will print the upper half of the pattern for (numberOfRows = size; numberOfRows >= 1; numberOfRows--) { for (numberOfColumns = 1; numberOfColumns <= numberOfRows; numberOfColumns++) System.out.print(numberOfRows); // for printing the space characters if (numberOfRows != size) { for (k = 1; k <= spaces; k++) System.out.print(" "); spaces = spaces + 2; } // for displaying the corresponding values for (numberOfColumns = numberOfRows; numberOfColumns >= 1; numberOfColumns--) if (numberOfColumns != size) System.out.print(numberOfRows); System.out.println(); } spaces = spaces - 4; // This will print the lower half of the pattern for (numberOfRows = 2; numberOfRows <= size; numberOfRows++) { for (numberOfColumns = 1; numberOfColumns <= numberOfRows; numberOfColumns++) System.out.print(numberOfRows); // for displaying the space character in the lower half if (numberOfRows != size) { for (k = 1; k <= spaces; k++) System.out.printf(" "); spaces = spaces - 2; } // for displaying the corresponding values for (numberOfColumns = numberOfRows; numberOfColumns >= 1; numberOfColumns--) if (numberOfColumns != size) System.out.print(numberOfRows); System.out.println(); } } }
Output : Enter the number of rows : 5 555555555 4444 4444 333 333 22 22 1 1 22 22 333 333 4444 4444 555555555
C Code to Print Hollow Numerical Parallelogram
#include <stdio.h> #include <stdlib.h> int main() { int size , numberOfRows , numberOfColumns,k = 0, spaces = 1; printf("Enter the number of rows : "); scanf ("%d",&size); for (numberOfRows = size; numberOfRows >= 1; numberOfRows--) { for (numberOfColumns = 1; numberOfColumns <= numberOfRows; numberOfColumns++) printf("%d",numberOfRows); if (numberOfRows != size) { for (k = 1; k <= spaces; k++) printf(" "); spaces = spaces + 2; } for (numberOfColumns = numberOfRows; numberOfColumns >= 1; numberOfColumns--) if (numberOfColumns != size) printf("%d",numberOfRows); printf("\n"); } spaces = spaces - 4; for (numberOfRows = 2; numberOfRows <= size; numberOfRows++) { for (numberOfColumns = 1; numberOfColumns <= numberOfRows; numberOfColumns++) printf("%d",numberOfRows); if (numberOfRows != size) { for (k = 1; k <= spaces; k++) printf(" "); spaces = spaces - 2; } for (numberOfColumns = numberOfRows; numberOfColumns >= 1; numberOfColumns--) if (numberOfColumns != size) printf("%d",numberOfRows); printf("\n"); } return 0; }
Output : Enter the number of rows : 5 555555555 4444 4444 333 333 22 22 1 1 22 22 333 333 4444 4444 555555555
C++ Code to Print Hollow Numerical Parallelogram
#include <bits/stdc++.h> using namespace std; int main() { int size , numberOfRows , numberOfColumns,k = 0, spaces = 1; printf("Enter the number of rows : "); scanf ("%d",&size); for (numberOfRows = size; numberOfRows >= 1; numberOfRows--) { for (numberOfColumns = 1; numberOfColumns <= numberOfRows; numberOfColumns++) cout << numberOfRows; if (numberOfRows != size) { for (k = 1; k <= spaces; k++) cout << " "; spaces = spaces + 2; } for (numberOfColumns = numberOfRows; numberOfColumns >= 1; numberOfColumns--) if (numberOfColumns != size) cout << numberOfRows ; cout << "\n" ; } spaces = spaces - 4; for (numberOfRows = 2; numberOfRows <= size; numberOfRows++) { for (numberOfColumns = 1; numberOfColumns <= numberOfRows; numberOfColumns++) cout << numberOfRows ; if (numberOfRows != size) { for (k = 1; k <= spaces; k++) cout << " "; spaces = spaces - 2; } for (numberOfColumns = numberOfRows; numberOfColumns >= 1; numberOfColumns--) if (numberOfColumns != size) cout << numberOfRows ; cout << "\n" ; } return 0; }
Output : Enter the number of rows : 5 555555555 4444 4444 333 333 22 22 1 1 22 22 333 333 4444 4444 555555555
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: