Saturday, March 4, 2023
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++

  

#embody <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 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 Area:  O(1) since we aren’t utilizing any additional house

Previous articleGRE examination Payment
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments