- C Program to design a digital clock
>>> In this example, we will see a C program to design a digital clock.
>>> In this program the clock will start with the time 00:00:00 and then it will work like a digital clock where it will show the time with hour, minutes and second.
This program will generate a digital clock using c program. The logic behind to implement this program,
- Initialize hour, minute, seconds with 0.
- Run an infinite loop.
- Increase second and check if it is equal to 60 then increase minute and reset second to 0.
- Increase minute and check if it is equal to 60 then increase hour and reset minute to 0.
- Increase hour and check if it is equal to 24 then reset hour to 0.
_____________________________________
Coding
#include<stdio.h>
#include<windows.h>
int main()
{
system("color 3f");
int h,m,s;
int d=1000;//add a delay of 1000 milisecoond and will use in main function
printf("set time :\n");
scanf("%d%d%d",&h,&m,&s);
if(h>12 || m>60 || s>60)
{
printf("ERROR!!!!!\n");
exit(0);
}
while(1)//this is the infinite loop and anything inside it continues run
{
s++;
if(s>59)
{
m++;
s=0;
}
if(m>59)
{
h++;
m=0;
}
if(h>12)
{
h=1;
}
printf("\n CLOCK:");
printf("\n %02d:%02d:%02d",h,m,s);//this writes our time in this format 00:00:00
Sleep(d);//this function sleep slows down while the loop and make it more real clock
system("cls");//this clear the screen
}
}
_____________________________________
Explanation of solution
>> In The first line we declare the required header file (stdio.h and windows.h)
we declare windows.h because inside this many of of our c functions are declare thats why we declare.
>> system("color 3f"); This means your output screen into a good colour recommended to use this.
>> Next we declare our variable (H,M,S) H stands for Hour ,M stands for Minutes, and S stands for seconds.
>> Then we declare a "d" variable for delay to 1000 mill seconds, after this we use the printf function to ask user to enter the time ,then by the help of scanf we takes the input.
>> Then we make if conditional statement where we say that if user does not enter a valid time This will show an error and the program will terminate by the help of exit function.
>> Next here run an infinite loop that that will show our Digital clock.
>> In first condition we increment seconds ,then if user enter the second less than 59 we increment the Minutes and initialize the seconds in 0.
>>Then in the next condition we check that if the entered minutes is less than 59 increment the hour and initialize minutes into 0;
>>Then in the next condition we check that if the hour is less than 12 and initialize from 1.
>>Now we print the Time in the output screen and we use here clear screen in last because we want the updated time.
OUTPUT
>> after that it will update time like the above picture.
Also check Out
Q-2) pointers in c
Q-3) Vector Sort
Also check Out Projects for Resume
Projects on C++
Projects on C
Problems On C
Q-1) Simple Calculator
Problems On C++
Q-2) Vectors In c++
THANK YOU !!!
- ---> And that is for this article friends hope all of you enjoy this
- article if still you have any doubts please comment below
- i will try to solve that .
Post a Comment
Post a Comment
Please do not spam in comment