Print Full Pyramid of Number Pattern
Java number pyramid: In the previous article, we have discussed Java Program to Print Downward Triangle with Increasing Order Number Pattern
In this article we are going to see how to print full pyramid number pattern.
- Java Code to Print Full Pyramid of Number Pattern
- C Code to Print Full Pyramid of Number Pattern
- C++ Code to Print Full Pyramid of Number Pattern
Example-1 When rows value = 5 1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4 5 6 7 8 9 8 7 6 5
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 number of rows and store it in an integer variable
rows.
- Take one outer for loop to iterate the rows.
- Take two inner loops, one for loop to print the space and another while loop to print number.
- After each iteration print a new line.
Java Code to Print Full Pyramid of 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 total number of rows as input from user System.out.print("Rows : "); int rows= scan.nextInt(); //Row and column are the iterators, temp1 and temp2 are holders that hold //valuye after each iteration int numberOfRows, numberOfColumns, temp1=0, temp2=0; //Outer loop to iterate the rows //Iterates from 1 to the number of rows entered by the user for (numberOfRows = 1; numberOfRows <= rows; ++numberOfRows) { //Inner loop to print space for (numberOfColumns = 1; numberOfColumns <= rows-numberOfRows; ++numberOfColumns) { System.out.print(" "); ++temp1; } numberOfColumns = 0; //Inner loop to print number while(numberOfColumns!=2*numberOfRows-1) { if(temp1<=rows-1) { System.out.print((numberOfRows+numberOfColumns)+" "); ++temp1; } else { ++temp2; System.out.print((numberOfRows+numberOfColumns-2*temp2)+" "); } ++numberOfColumns; } //Resets the variables to 0 after each iteration temp2 = temp1 = numberOfColumns = 0; //Prints a newline System.out.println(); } } }
Output: Rows : 7 1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4 5 6 7 8 9 8 7 6 5 6 7 8 9 10 11 10 9 8 7 6 7 8 9 10 11 12 13 12 11 10 9 8 7
C Code to Print Full Pyramid of Number Pattern
#include <stdio.h> int main() { //Taking total number of rows as input from user printf("Rows : "); int rows; scanf("%d", &rows); //Row and column are the iterators, temp1 and temp2 are holders that hold //valuye after each iteration int numberOfRows, numberOfColumns, temp1 = 0, temp2 = 0; //Outer loop to iterate the rows //Iterates from 1 to the number of rows entered by the user for (numberOfRows = 1; numberOfRows <= rows; ++numberOfRows) { //Inner loop to print space for (numberOfColumns = 1; numberOfColumns <= rows - numberOfRows; ++numberOfColumns) { printf(" "); ++temp1; } numberOfColumns = 0; //Inner loop to print number while (numberOfColumns != 2 * numberOfRows - 1) { if (temp1 <= rows - 1) { printf("%d ", (numberOfRows + numberOfColumns)); ++temp1; } else { ++temp2; printf("%d ", (numberOfRows + numberOfColumns - 2 * temp2)); } ++numberOfColumns; } //Resets the variables to 0 after each iteration temp2 = temp1 = numberOfColumns = 0; //Prints a newline printf("\n"); } return 0; }
Output: Rows : 7 1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4 5 6 7 8 9 8 7 6 5 6 7 8 9 10 11 10 9 8 7 6 7 8 9 10 11 12 13 12 11 10 9 8 7
C++ Code to Print Full Pyramid of Number Pattern
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { //Taking total number of rows as input from user cout << "Rows : "; int rows; cin >> rows; //Row and column are the iterators, temp1 and temp2 are holders that hold //valuye after each iteration int numberOfRows, numberOfColumns, temp1 = 0, temp2 = 0; //Outer loop to iterate the rows //Iterates from 1 to the number of rows entered by the user for (numberOfRows = 1; numberOfRows <= rows; ++numberOfRows) { //Inner loop to print space for (numberOfColumns = 1; numberOfColumns <= rows - numberOfRows; ++numberOfColumns) { cout << " "; ++temp1; } numberOfColumns = 0; //Inner loop to print number while (numberOfColumns != 2 * numberOfRows - 1) { if (temp1 <= rows - 1) { cout << (numberOfRows + numberOfColumns) << " "; ++temp1; } else { ++temp2; cout << (numberOfRows + numberOfColumns - 2 * temp2) << " "; } ++numberOfColumns; } //Resets the variables to 0 after each iteration temp2 = temp1 = numberOfColumns = 0; //Prints a newline cout << endl; } return 0; }
Output: Rows : 7 1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4 5 6 7 8 9 8 7 6 5 6 7 8 9 10 11 10 9 8 7 6 7 8 9 10 11 12 13 12 11 10 9 8 7
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: