Timer program in C/C++

Timer program in C/C++

A simple timer tells you when the time you mentioned is up. So this program will accept the time you enter and give a beep sound after the countdown is over. Let’s see a basic timer program in C and C++.

1. Output-

timer program in c

2. Program in C :

#include<stdio.h>
#include<unistd.h>

void countdown(int, int, int);
int main() {
  int h, m, s;
  printf("* Enter stopwatch time in HH MM SS format: (e.g: 00 01 10 - 1 minute 10 seconds) *\n HH MM SS = ");
  scanf("%d%d%d", &h, &m, &s);
  countdown(h, m, s);
  while (m > 0) {
    system("cls");
    m--;
    s = 59;
    countdown(h, m, s);
  }
  while (h > 0) {
    system("cls");
    h--;
    m = 59;
    s = 59;
    countdown(h, m, s);
    while (m > 0) {
      m--;
      s = 59;
      countdown(h, m, s);
    }
  }
  printf(" Time up!\a");
  return 0;
}
void countdown(int h, int m, int s) {
  while (s >= 0) {
    system("cls");
    printf("%02d:%02d:%02d", h, m, s);
    sleep(1);
    s--;
  }
}

3. Program in C++ :

#include<iostream>
#include<unistd.h>
#include<iomanip>

void countdown(int, int, int);
using namespace std;
int main() {
  int h, m, s;
  cout << "* Enter stopwatch time in HH MM SS format: (e.g: 00 01 10 - 1 minute 10 seconds) *\n HH MM SS = ";
  cin >> h >> m >> s;
  countdown(h, m, s);
  while (m > 0) {
    system("cls");
    m--;
    s = 59;
    countdown(h, m, s);
  }
  while (h > 0) {
    system("cls");
    h--;
    m = 59;
    s = 59;
    countdown(h, m, s);
    while (m > 0) {
      m--;
      s = 59;
      countdown(h, m, s);
    }
  }
  cout << " Time up!\a";
  return 0;
}
void countdown(int h, int m, int s) {
  while (s >= 0) {
    system("cls");
    cout << setfill('0') << setw(2) << h << ":" << setw(2) << m << ":" << setw(2) << s;
    sleep(1);
    s--;
  }
}

4. Explanation-

Firstly values for time are taken as input. while loops are used to check the time and decrement accordingly. So when the seconds become zero, 1 minute is reduced by making the seconds value 59. Similarly, 1 hour is reduced and minutes value is set to 59. We used “%02d” and setfill(‘0’) with setw(2) to print hours, seconds, date and month in 2-digit format. Then we used a function to print the countdown time. We have used ‘sleep‘ function for 1 second and then the screen is cleared for new output. This continues till we close output screen. <unistd.h> library is needed in C/C++ for sleep(number_of_seconds); Finally when hours, minutes and seconds become ‘0’, “Time up!” message is displayed with a beep sound. This sound is added using escape sequence “\a”.

Note: “%02d” in C displays the integer in 2-digit format. setw(x) function in C++ is a function which displays any integer in ‘x’ space. But instead of blank spaces we can use setfill(character_to_be_filled). We need <iomanip> library for this in C++. So using “%02d” and setfill(‘0’) with setw(2), if integer is 5 (single digit) then 5 will be printed in a 2-digit format i.e ’05’.

5. Parts of program-

1. #include is a pre-processor directive. It is used to include header files.
2. stdio/iostream is header file which has certain commands that c/c++ supports. E.g. return, main, etc.
3. .h is an extension for header file.
4. int is data type. It indicates that the program returns a value.
5. main() is main function that indicates the compiler that the user written programs starts from here.
6. printf/cout is the keyword use to print a message.
7. scanf/cin is the keyword use to store values in variables.
8. return statement is use to return a value. If any error occurs the program will return 0.

Also checkout- Current Date and Time Program in C/C++

Conclusion-

So this was the basic Timer program in C/C++. We hope it was clear to you. If you find it useful then do share it with your programming buddies and friends. Also if you have any doubt regarding any post do tell us in the comment section.

For daily updates do follow us on Instagram. Visit again!

ErrorFreeProgram.

Leave a Reply

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