Monday, November 13, 2017

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.

Output Format:
N lines containing the desired pattern.

Boundary Conditions: 2 <= N <= 100

Example Input/Output 1:
Input:
3
Output:
1 1 1 2
3 2 2 2
3 3 3 4

Solution:
#include <stdio.h>
void printPattern(int n)
{
 for(int i=1;i<=n;i++)
 {
     for(int j=0;j<=n;j++)
      if(i%2==1&&j==n||i%2==0&&j==0)
       printf("%d ",i+1);
      else
       printf("%d ",i);
      printf("\n");
 }
}
int main()
{
int N;
scanf("%d",&N);
printPattern(N);
}
Share: