HomeSoftware DevelopmentProgram to print diamond sample utilizing numbers and stars

Program to print diamond sample utilizing numbers and stars


Enhance Article

Save Article

Like Article

Enhance Article

Save Article

Program to print the next sample of a half diamond for N.

Sample for N = 4

Instance:

Enter: N = 5
Output: 

1

2*3

4*5*6

7*8*9*10

11*12*13*14*15

11*12*13*14*15

7*8*9*10

4*5*6

2*3

1

This program is split into 4 components.

C++

  

#embrace <iostream>

utilizing namespace std;

  

void sample(int N)

{

    int i, j, rely = 1;

  

    

    for (i = 1; i <= N; i++) {

  

        

        

        for (j = 1; j <= i; j++) {

  

            if (j < i)

  

                cout << rely++ << "*";

  

            else

  

                cout << rely++;

        }

        cout << endl;

    }

    rely = rely - N;

  

    

    for (i = N; i >= 1; i--) {

  

        

        

        for (j = 1; j <= i; j++) {

  

            if (j < i)

  

                cout << rely++ << "*";

  

            else

  

                cout << rely++;

        }

        rely = (rely + 1) - 2 * i;

        cout << endl;

    }

}

  

int foremost()

{

    int N = 4;

  

    

    sample(N);

    return 0;

}

Output

1
2*3
4*5*6
7*8*9*10
7*8*9*10
4*5*6
2*3
1

Time Complexity: O(N2) 
Auxiliary House:  O(1) since we’re not utilizing any further house

RELATED ARTICLES

Most Popular

Recent Comments