Breaking

Thursday 28 September 2017

Pattern Printing - Start Number

                 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 1:
Input:
3 4
Output:
3
4 4
5 5 5
6 6 6 6
6 6 6 6
5 5 5
4 4
3
Example Input/Output 2:
Input:
7 9
Output:
7
8 8
9 9 9
10 10 10 10
11 11 11 11 11
12 12 12 12 12 12
13 13 13 13 13 13 13
14 14 14 14 14 14 14 14
15 15 15 15 15 15 15 15 15
15 15 15 15 15 15 15 15 15
14 14 14 14 14 14 14 14
13 13 13 13 13 13 13
12 12 12 12 12 12
11 11 11 11 11
10 10 10 10
9 9 9
8 8
7
Code:

Java:


import java.util.Scanner;

public class Hello {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int start = sc.nextInt();
        int N = sc.nextInt();
        printPattern(start, N);
         
    }

static void printPattern(int base, int N){
int i,j,x=0,flag=0;
for(i=0;i<=N*2+1;i++)
{
   if(i<=N)
        x=i;
    else   
        x--;
    for(j=0;j<x;j++)
    {
     System.out.print(base-1+" ");  
    }
    if(i<N)
    base++;
    else
    {
    if(flag==0)
       { i--;flag=1;}
    else
        base--;
    }
   
    System.out.println();
}
}

Please do comment If u have any Queries!

1 comment:

  1. void printPattern(int base, int N)
    {
    for(int i=1;i<=N;i++)
    {
    for(int j=0;j0;i--)
    {
    for(int j=0;j<i;j++)
    {
    printf("%d ",base);
    }
    printf("\n");
    base--;
    }
    }

    ReplyDelete

Like