Coders Packet

C++ program to print a star after a count down

By Rati Gupta

In this C++ program, a star is printed after the desired countdown given by the user. This star has been created with the help of several FOR loops.

Approach:-

Create the countdown with the help of the sleep() function.

Print the star when the counter becomes zero.

This star shape is made by combining three shapes :

1. The uppermost triangle of the star shape.

2. The upper half of the downside triangle.

3. Two downside obtuse triangles which are almost symmetric to each other.

4. All of the above three shapes can be made by using FOR loops and combining them will give a star shape.

    On changing the values of variables and constants used in the program, the size of the star shape can be changed.

 

 

#include 
#include 
using namespace std;
int main()
{

    int counter;
    cout<<"Enter countdown: ";
    cin>>counter;
    Sleep(1000);
    while (counter >= 0)
    {
        cout << "\rTime remaining: " << counter << flush;
        Sleep(1000);
        counter--;
    }
    counter++;
    cout<<endl;
     if(counter==0)            //condition for printing the star
   {
    int i,j,k,p=0,h=0;
    int m=10;
    for( i=1;i<=5;i++)          //loop for printing uppermost triangle
    {
        for( j=49;j>=i;j--)
        {
            cout<<" ";
        }
        for(k=1;k<=i+p;k++)
        {
            cout<<"*";
         }
        p+=1;
        cout<<endl;
    }
    for(i=11;i>=7;i--)               //loop for printing upper half of the downside triangle
    {
        for(j=43;j>=i;j--)
        {
            cout<<" ";
        }
        for(k=1;k<=3*i;k++)
        {

            cout<<"*";
        }
        cout<<endl<<" ";
    }
    for( i=11;i<=20;i++)          //loop for printing two downside obtuse triangles
    {
        for( j=47;j>=i;j--)
        {
            cout<<" ";
        }
        for(k=1;k<=m;k++)
        {
            cout<<"*";
        }
        cout<<" ";
               for(k=1;k<h;k++)   //for giving space in middle of stars
        {
            cout<<" ";

        }
        if(i>11)
          cout<<" ";
         for(k=1;k<=m;k++)
        {
            cout<<"*";
        }
          h+=4;
        m--;
        cout<<endl<<" ";
    }
   }
    return 0;
}

 

Output:-

Output

 

Download project

Reviews Report

Submitted by Rati Gupta (rati123)

Download packets of source code on Coders Packet