In this post, we will write a C++ program to print the diamond patterns of stars. the first user will enter the number between 1 to 25 and then the program will print the star pattern based on the number.

C++ program to print diamond pattern of stars


C++ program to print diamond patterns of stars

#include<iostream>
#include<conio.h>
#include<windows.h>

BOOL gotoxy(const WORD x, const WORD y) {
    COORD xy;
    xy.X = x;
    xy.Y = y;
    return SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), xy);
}

int main()
{
    system("cls");

    int i,j,k,l,n;

    std::cout<<"Enter the number of star b/w 1 to 25 = ";
    std::cin>>n;

system("cls");

    if(n <= 25)
    {
        for(i=1;i<=n;i++)
            for(j=(n+1)-i;j<=i;j++)
            {
                gotoxy(i+i-n,j);
                std::cout<<"*";
            }

        for(k=n;k>=1;k--)
            for(l=k;l<=(n+1)-k;l++)
            {
                gotoxy(k+k+(n-2),l);
                std::cout<<"*";
            }
    }
    else
        std::cout<<"The number you enter is greater than 25 ";

    getch();
    return 0;
}



Output

Enter the number of star b/w 1 to 25 = 25

                  *
                * * *
              * * * * *
            * * * * * * *
          * * * * * * * * *
        * * * * * * * * * * *
      * * * * * * * * * * * * *
    * * * * * * * * * * * * * * *
      * * * * * * * * * * * * *
        * * * * * * * * * * *
          * * * * * * * * *
            * * * * * * *
              * * * * *
                * * *
                  *