cplusplus
cplusplus

 

 

Flash sales are an effective marketing technique for driving quick sales, and a countdown timer can create a sense of urgency among customers. In this tutorial, we will demonstrate how to write a Flash Sale Countdown Timer program in C++.

Objective

The goal of this program is to display a countdown timer for a flash sale that provides real-time updates and notifies the user when the sale is about to end. The timer will decrement in seconds, showing the time remaining in a user-friendly format.

C++ Program Code

#include 
#include 
#include 

using namespace std;

void display_timer(int seconds) {
    int hours = seconds / 3600;
    int minutes = (seconds % 3600) / 60;
    int remaining_seconds = seconds % 60;

    cout << "Flash Sale Countdown: " 
         << hours << "h : " 
         << minutes << "m : " 
         << remaining_seconds << "s" << endl;
}

int main() {
    int flash_sale_duration = 3600;  // 1 hour in seconds

    cout << "Flash Sale Started!" << endl;
    cout << "Time Remaining: " << endl; while (flash_sale_duration > 0) {
        display_timer(flash_sale_duration);
        this_thread::sleep_for(chrono::seconds(1)); // Sleep for 1 second
        flash_sale_duration--;
    }

    cout << "Flash Sale Ended!" << endl;
    return 0;
}

Program Explanation

The program begins by initializing a countdown timer set to 3600 seconds (1 hour). The function display_timer() takes the total time in seconds and calculates the number of hours, minutes, and seconds remaining. It then prints this in a readable format.

The main() function runs a loop that continues until the timer reaches zero. Every second, the program updates the countdown and pauses for one second using this_thread::sleep_for() to make the countdown visible. When the timer reaches zero, the program outputs “Flash Sale Ended!” and terminates.

How to Run the Program

To run the Flash Sale Countdown Timer program in C++, follow these steps:

  1. Install a C++ compiler like GCC (GNU Compiler Collection) or use an IDE like Code::Blocks or Visual Studio.
  2. Create a new C++ file and paste the code into it. Save it as flash_sale_timer.cpp.
  3. Compile the program using the C++ compiler. If you’re using the command line, use the following command:
    g++ flash_sale_timer.cpp -o flash_sale_timer
  4. Run the compiled program by entering:
    ./flash_sale_timer
  5. The countdown timer will start, and you will see the time remaining for the flash sale in hours, minutes, and seconds.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)