Thursday, September 28, 2017

Pattern Printing - Start Number

Given an integer N as the input and a start integer S, print the pattern as given in the Example Input/Output section.

Input Format:
The first line contains S and N, each separated by a space.

Output Format:
2N lines containing the desired pattern.

Boundary Conditions: 2 <= N <= 50 1 <= S <= 20

Example Input/Output:
Input:
3 4
Output:
3
4 4
5 5 5
6 6 6 6
6 6 6 6
5 5 5
4 4
3

Solution:
#include<stdio.h>
void printPattern(int b,int n)
{
    for(int i=0;i<2*n;i++)
    {
        int k=i<n?b+i:(n-i%n)+b-1;
        for(int j=0;j<=k-b;j++)
         printf("%d ",k);
        printf("\n");
    }
}
int main()
{
    int start, N;
    scanf("%d%d",&start,&N);
    printPattern(start,N);
    return 0;
}


Share: