Software Development

Program to print diamond sample utilizing numbers and stars

Program to print diamond sample utilizing numbers and stars
Written by admin


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 elements.

C++

  

#embrace <iostream>

utilizing namespace std;

  

void sample(int N)

{

    int i, j, depend = 1;

  

    

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

  

        

        

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

  

            if (j < i)

  

                cout << depend++ << "*";

  

            else

  

                cout << depend++;

        }

        cout << endl;

    }

    depend = depend - N;

  

    

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

  

        

        

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

  

            if (j < i)

  

                cout << depend++ << "*";

  

            else

  

                cout << depend++;

        }

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

        cout << endl;

    }

}

  

int predominant()

{

    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 Area:  O(1) since we’re not utilizing any additional area

About the author

admin

Leave a Comment