Java Program to Print Square with Repeated Number Decreasing Order Pattern

Printing Square with Repeated Number Decreasing Order Pattern

In the previous article, we have discussed Java Program to Print Square with Repeated Number Increasing Order Pattern

In this program we are going to see how to print the square with repeated number decreasing number pattern.

Example-1

When size value=5 and 
starting number = 9

9 9 9 9 9
8 8 8 8 8
7 7 7 7 7
6 6 6 6 6
5 5 5 5 5
Example-2:

When size value=3 and 
starting number = 5

5 5 5
4 4 4
3 3 3

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 total size and number store them in integer variables size & num .
  • Take one outer for loop to iterate the rows.
  • Take one inner for loop to iterate the columns and print the column values.
  • After each iteration print a new line.

Java Code to Print Square with Repeated Number Decreasing Order 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 0 to one less than the size entered by the user
        for (numberOfColumns = 0; numberOfColumns < size; numberOfColumns++)
        {
            //Prints the num value
            System.out.print(num+" ");
        }
        //Incrementing the num variable after each row
        num--;
        //Prints a newline
        System.out.println();
    }
}
}

Output:

Size of square : 5
Number to print from : 9

9 9 9 9 9 
8 8 8 8 8 
7 7 7 7 7 
6 6 6 6 6 
5 5 5 5 5

C Code to Print Square with Repeated Number Decreasing Order 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 0 to one less than the size entered by the user
        for (numberOfColumns = 0; numberOfColumns < size; numberOfColumns++)
        {
            //Prints the num value
            printf("%d ", num);
        }
        //Incrementing the num variable after each row
        num--;
        //Prints a newline
        printf("\n");
    }
    return 0;
}

Output:

Size of square : 5
Number to print from : 9

9 9 9 9 9 
8 8 8 8 8 
7 7 7 7 7 
6 6 6 6 6 
5 5 5 5 5

C++ Code to Print Square with Repeated Number Decreasing Order 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 = 0; numberOfColumns < size; numberOfColumns++)
        {
            //Prints the num value
            cout << num << " ";
        }
        //Incrementing the num variable after each row
        num--;
        //Prints a newline
        cout << endl;
    }
    return 0;
}

Output:

Size of square : 5
Number to print from : 9

9 9 9 9 9 
8 8 8 8 8 
7 7 7 7 7 
6 6 6 6 6 
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: