Matrix Pattern Printing - First / Last Higher
Given an integer N as the input, print the pattern as given in the Example Input/Output section.
Input Format:
The first line contains N.
The first line contains N.
Output Format:
N lines containing the desired pattern.
N lines containing the desired pattern.
Boundary Conditions:
2 <= N <= 100
2 <= N <= 100
Example Input/Output 1:
Input:
3
Input:
3
Output:
1 1 1 2
3 2 2 2
3 3 3 4
1 1 1 2
3 2 2 2
3 3 3 4
Code:
#include<stdio.h>
void printPattern(int N)
{
int i,j;
for(i=0;i<N;i++)
{
for(j=0;j<=N;j++)
{
if(i%2==0)
{
if(j==N)
printf("%d ",i+2);
else
printf("%d ",i+1);
}
else
{
if(j==0)
printf("%d
",i+2);
else
printf("%d
",i+1);
}
}
printf("\n");
}
}
int main()
{
int N;
scanf("%d",&N);
printPattern(N);
}
C++:
#include<iostream>
void printPattern(int N)
{
int i,j;
for(i=0;i<N;i++)
{
for(j=0;j<=N;j++)
{
if(i%2==0)
{
if(j==N)
cout<<i+2<<” “;
else
cout<<i+1<<” “;
}
else
{
if(j==0)
cout<<i+2<<” “;
else
cout<<i+1<<”
“;
}
}
cout<<"\n";
}
}
int main()
{
int N;
cin>>N;
printPattern(N);
}
Please do comment If u have any Queries!
No comments:
Post a Comment