vendredi 6 septembre 2019

Advanced Number Pattern

Given a Number N, Print the following pattern.

Input Format

The input contains a number N

Constraints

1 < N < 100

Output Format

The required pattern for input 5 is

1 2 9 3 8 10 4 7 11 14 5 6 12 13 15

and for input 3 is

1 2 5 3 4 6

this is the code i have tried .. but the results are not the same

#include<stdio.h>
void pattern(int n)
{
    for(int i=1; i<=n; i++)
    {
        int k = i;
        for(int j=1; j<=i; j++)
        {
            printf("%d ",k);
            k =  n - j + k;

        }
        printf("\n");
    }
}
int main()
{

    int n = 5;
    pattern(n);
    return 0;
}



this is the result of the above code

1
2 6
3 7 10
4 8 11 13
5 9 12 14 15

How should I modify the above code to get the Expected Output?

thanks in advanced ....

Aucun commentaire:

Enregistrer un commentaire