Program to Print Square with Same Number Pattern
In the previous article, we have discussed Java Program to Print Square with Row wise Increasing Number Pattern
In this program we are going to see how to print the square with same number pattern.
- Java Code to Print Square with Same Number Pattern
- C Code to Print Square with Same Number Pattern
- C++ Code to Print Square with Same Number Pattern
Example-1 When size value=5 and num =9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
Example-2: When size value=3 and num =3 5 5 5 5 5 5 5 5 5
Now, let’s see the actual program to print it.
Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.
Approach:
- Enter total size and number then store it 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 newline.
Java Code to Print Square with Same Number Pattern
import java.util.Scanner; class Main { public static void main(String[] args){ //Taking size as input from user System.out.print("Size of square : "); Scanner scan = new Scanner(System.in); int size = scan.nextInt(); //Taking num as input from user System.out.print("Number to print : "); int num = scan.nextInt(); //Row and column are the iterators int row, col; //Outer loop to iterate the rows //Iterates from 1 to the size entered by the user for(row=1;row<=size;row++) { //Inner loop to iterate the columns //Iterates from 0 to one less than the size entered by the user for (col = 0; col < size; col++) { //Prints the num value System.out.print(num+" "); } //Prints a newline System.out.println(); } } }
Output: Size of square : 5 Number to print : 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
C Code to Print Square with Same Number 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 : "); int num; scanf("%d", &num); //Row and column are the iterators int row, col; //Outer loop to iterate the rows //Iterates from 1 to the size entered by the user for (row = 1; row <= size; row++) { //Inner loop to iterate the columns //Iterates from 0 to one less than the size entered by the user for (col = 0; col < size; col++) { //Prints the num value printf("%d ", num); } //Prints a newline printf("\n"); } return 0; }
Output: Size of square : 5 Number to print : 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
C++ Code to Print Square with Same Number Pattern
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { //Taking size as input from user cout << "Size of square : "; int size; cin >> size; //Taking number as input from user cout << "Number to print : "; int num; cin >> num; //Row and column are the iterators int row, col; //Outer loop to iterate the rows //Iterates from 1 to the size entered by the user for (row = 1; row <= size; row++) { //Inner loop to iterate the columns //Iterates from 0 to one less than the size entered by the user for (col = 0; col < size; col++) { //Prints the num value cout << " " << num; } //Prints a newline cout << endl; } return 0; }
Output: Size of square : 5 Number to print : 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
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: