Pages

Sunday, March 30, 2025

Diamond Pattern problem in C++

 #include <iostream>

using namespace std;
// quote- break down bigger problems into smaller problems
// Diamond pattern -


/*
pseudocode
1. build first pyramid
then second pyramid
*/
int main() {
    int n = 4;

    // first pyramid
    for(int i = 1; i <= n; i++){
        // spaces
        for(int j = 1; j <= n-i; j++){
            cout << " ";
        }
        // stars
        for(int j = 1; j <= 2*i-1; j++){
            cout << "*";
        }
        cout << endl;
       
    }
    // second pyramid
    for(int i = n; i >= 1; i--){

        // spaces
        for(int j = 1; j <= n-i; j++){
            cout << " ";
        }
        // stars
        for(int j = 1; j <= 2*i-1; j++){
            cout << "*";
        }
       

        cout << endl;

    }

    return 0;

}

OUTPUT-
* *** ***** ******* ******* ***** ***
*

No comments:

Post a Comment

remove duplicates from sorted array - two pointer approach (leetcode)

  class Solution {     public int removeDuplicates ( int [] nums ) {         // base case: return if array have no el.         if ( nums...