Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

C++ program to print diamond pattern of stars

YASH PAL, 31 July 202422 August 2024

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

                  *
                * * *
              * * * * *
            * * * * * * *
          * * * * * * * * *
        * * * * * * * * * * *
      * * * * * * * * * * * * *
    * * * * * * * * * * * * * * *
      * * * * * * * * * * * * *
        * * * * * * * * * * *
          * * * * * * * * *
            * * * * * * *
              * * * * *
                * * *
                  *
coding problems cpp

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes