Printing Square with Repeated Number Increasing Order Pattern
In the previous article, we have discussed Java Program to Print Square with Alternate row Binary Number Pattern
In this program we are going to see how to print the square with repeated number increasing number pattern.
- Java Code to Print Square with Repeated Number Increasing Order Pattern
- C Code to Print Square with Repeated Number Increasing Order Pattern
- C++ Code to Print Square with Repeated Number Increasing Order Pattern
Example-1 When size value=5 and starting number = 1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5
Example-2: When size value=3 and starting number = 5 5 5 5 6 6 6 7 7 7
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 starting number and store them in integer variables
size&numrespectively.. - Take one outer for loop to iterate the rows.
- Take one inner for loop to iterate the columns and print the column values i.e the numbers.
- After each iteration print a new line.
Java Code to Print Square with Repeated Number Increasing 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 : 3 Number to print from : 5 5 5 5 6 6 6 7 7 7
C Code to Print Square with Repeated Number Increasing 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 : 3 Number to print from : 5 5 5 5 6 6 6 7 7 7
C++ Code to Print Square with Repeated Number Increasing 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 : 3 Number to print from : 5 5 5 5 6 6 6 7 7 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: