Printing Cross Number Pattern
In the previous article, we have discussed Java Program to Print Pascal’s Triangle Number Pattern
In this article we are going to see how to print the cross number pattern in java. What is number pattern program in java, cross pattern in java, Java Program To Print Cross Pattern, number patterns in java, Number Pattern Printing In Java, java code pattern, Xylem And Phloem Number In Java, Xylem And Phloem Program In Java, get the points in detail manner.
- Java Code to Print Cross Number Pattern
- C Code to Print Cross Number Pattern
- C++ Code to Print Cross Number Pattern
Example-1 When number value = 5 1 1 2 2 3 3 4 4 5 4 4 3 3 2 2 1 1
Example-2: When number value=7 1 1 2 2 3 3 4 4 5 5 6 6 7 6 6 5 5 4 4 3 3 2 2 1 1
Now, let’s see the actual program to print it.
The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.
Approach:
- Enter the number to print up to and store it in an integer variable
number
. - 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.
Recommended Reading On: Java Program to Print Pant Number Pattern (Second Approach)
Java Code to Print Cross 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 to print as input from user System.out.print("Number to print upto : "); int number = scan.nextInt(); //Row and column are the iterators, space is the number of space //tempHolder is the pattern lines holder and numHolder iterates the value int numberOfRows, numberOfColumns, tempHolder = number * 2 - 1, numHolder = 1; //Outer loop to iterate the rows //Iterates from 1 to tempHolder(i.e. pattern lines) for (numberOfRows = 1; numberOfRows <= tempHolder; numberOfRows++) { //Inner loop to iterate the columns and print the number //Iterates from 1 to tempHolder(i.e. pattern lines) for (numberOfColumns = 1; numberOfColumns <= tempHolder; numberOfColumns++) { if (numberOfColumns == numberOfRows || numberOfColumns == tempHolder - numberOfRows + 1) System.out.print(numHolder); System.out.print(" "); } //Prints a newline System.out.println(); //Logic to change the numholder value after each iteration numHolder = (numberOfRows <= tempHolder / 2) ? ++numHolder : --numHolder; } } }
Output: Number to print upto : 6 1 1 2 2 3 3 4 4 5 5 6 5 5 4 4 3 3 2 2 1 1
C Code to Print Cross Number Pattern
#include <stdio.h> int main() { //Taking total number to print as input from user printf("Number to print upto : "); int number; scanf("%d", &number); //Row and column are the iterators, space is the number of space //tempHolder is the pattern lines holder and numHolder iterates the value int numberOfRows, numberOfColumns, tempHolder = number * 2 - 1, numHolder = 1; //Outer loop to iterate the rows //Iterates from 1 to tempHolder(i.e. pattern lines) for (numberOfRows = 1; numberOfRows <= tempHolder; numberOfRows++) { //Inner loop to iterate the columns and print the number //Iterates from 1 to tempHolder(i.e. pattern lines) for (numberOfColumns = 1; numberOfColumns <= tempHolder; numberOfColumns++) { if (numberOfColumns == numberOfRows || numberOfColumns == tempHolder - numberOfRows + 1) printf("%d", numHolder); printf(" "); } //Prints a newline printf("\n"); //Logic to change the numholder value after each iteration numHolder = (numberOfRows <= tempHolder / 2) ? ++numHolder : --numHolder; } return 0; }
Output Number to print upto : 6 1 1 2 2 3 3 4 4 5 5 6 5 5 4 4 3 3 2 2 1 1
C++ Code to Print Cross Number Pattern
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { //Taking total number to print as input from user cout << "Number to print upto : "; int number; cin >> number; //Row and column are the iterators, space is the number of space //tempHolder is the pattern lines holder and numHolder iterates the value int numberOfRows, numberOfColumns, tempHolder = number * 2 - 1, numHolder = 1; //Outer loop to iterate the rows //Iterates from 1 to tempHolder(i.e. pattern lines) for (numberOfRows = 1; numberOfRows <= tempHolder; numberOfRows++) { //Inner loop to iterate the columns and print the number //Iterates from 1 to tempHolder(i.e. pattern lines) for (numberOfColumns = 1; numberOfColumns <= tempHolder; numberOfColumns++) { if (numberOfColumns == numberOfRows || numberOfColumns == tempHolder - numberOfRows + 1) cout << numHolder; cout << " "; } //Prints a newline cout << endl; //Logic to change the numholder value after each iteration numHolder = (numberOfRows <= tempHolder / 2) ? ++numHolder : --numHolder; } return 0; }
Output: Number to print upto : 6 1 1 2 2 3 3 4 4 5 5 6 5 5 4 4 3 3 2 2 1 1
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.
Must check how much you gain by solving these:
Number Pattern Questions In Java
- Number Pattern Java
- How To Print Number Pattern In Java
- Number Pattern Programs In Java
- Number Pattern Print In Java
- Java Program For Number Pattern
- Print Numeric Pattern In Java
- Printing Number Pattern In Java
- Java Code For Number Pattern
Related Java Number Pattern Programs: