Number Pattern - Square number pattern - 10
Given two integers N1 and N2, display the given number pattern(Square number patterns).
in zinger code with comment please
Input:
5
5
where:
Output:
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
Note that you have'nt mentioned any language.So I have coded it in C++.Although you can change it in any language if you understand the algorithm.
//Code starts here.
#include <iostream>
using namespace std;
int main()
{
int N1,N2; //Integers for N1 and N2
cin>>N1>>N2; //Taking input for N1 and N2
int i,j; //Variables for loops
for(i=1;i<=N1;i++) //Outer loop,will run for N1(no. of rows)
times
{
if(i!=1&&i!=N1) //Print extra space every row except first
and last
cout<<" ";
for(j=1;j<=N2;j++) //Inner loop,will run for N2(no. of columns)
times
{
cout<<i<<" "; //Print value of i
}
cout<<endl; //Change line after every row
}
return 0;
}
//Code ends here
Output
Python code
#Code starts here
#First take N1 and N2 as input after converting into int
N1 =int(input())
N2 =int(input())
for i in range(1,N1+1): #will run from 1 to N1(no of rows)
if i!=1 and i!=N1: #For empty spaces except first and last
row
print(end=' ')
for j in range(1,N2+1): #will run for N2 times(no. of
columns)
print (i,end=' ')
print() #Line change
#End of program
Output for two runs
Get Answers For Free
Most questions answered within 1 hours.