Code For Alls..!

get the code!!

Wednesday 13 September 2017

Matrix Pattern Printing - First / Last Higher

                                         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
C program:
#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);
}


No comments:

Post a Comment