ADVERTISEMENT
Clock
Clock Application
This program takes user input for a time in HH:MM format, validates the input, and then determines which time-related phrases should be included in the output based on the entered time. The final result is printed on the console.
#include<stdio.h>
#include<stdlib.h>
main()
{
char *temp[]={"Half","Ten","Quarter","Twenty","Five","Minutes","To","Past","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","O' Clock"};
char time[5],substr[3][3];
int i=0,j=0,k=0,hour,min,s[21]={0};
printf("Enter time in HH:MM format like 01:15 in 12 hour format\n");
gets(time);
while(time[k]!='\0')
{
j=0;
while(time[k]!=':'&&time[k]!='\0')
{
substr[i][j]=time[k];
k++;
j++;
}
substr[i][j]='\0';
i++;
if(time[k]!='\0')
{
k++;
}
}
int len=i;
hour=atoi(substr[0]);
min=atoi(substr[1]);
if(hour<1||hour>12||min<0||min>59)
{
printf("Enter Valid Time\n");
exit(0);
}
else
{
if((min>=5&&min<10)||(min>=55&&min<=59))
{
s[4]=1;
}
if((min>=10&&min<15)||(min>=50&&min<55))
{
s[1]=1;
}
if((min>=15&&min<20)||(min>=45&&min<50))
{
s[2]=1;
}
if((min>=20&&min<25)||(min>=40&&min<45))
{
s[3]=1;
}
if(min>=25&&min<40)
{
s[0]=1;
}
if(min>=1&&min<=30)
{
if(min>=5)
{
s[7]=1;
}
s[hour+7]=1;
}
if(min>30&&min<=59)
{
s[6]=1;
if(s[2]!=1)
{
s[5]=1;
}
if(hour!=12)
s[hour+8]=1;
else
s[8]=1;
}
if(min>=0&&min<5)
{
s[hour+7]=1;
s[20]=1;
}
}
for(i=0;i<21;i++)
{
if(s[i]==1)
{
printf("%s ",temp[i]);
}
}
printf("\n");
}C Programming ExamplesEnter time in HH:MM format like 01:15 in 12 hour format
2:15
Quarter Past Two
C Programming ExamplesLet’s break down the code step by step:
- Include Header Files:
#include<stdio.h>
#include<stdlib.h>
These are standard C library header files. <stdio.h> is for input/output functions, and <stdlib.h> for memory allocation functions.
- Main Function:
main()
{
In older C standards, the main() function is used without specifying the return type (implicitly considered int).
- Declare Arrays and Variables:
char *temp[] = {"Half","Ten","Quarter","Twenty","Five","Minutes","To","Past","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","O' Clock"};
char time[5], substr[3][3];
int i = 0, j = 0, k = 0, hour, min, s[21] = {0};
Arrays are declared to store strings and integers. temp[] holds strings representing various time-related phrases, and s[] is an array of flags to indicate which phrases to include in the final output. Variables are declared to store time components and loop indices.
- User Input:
printf("Enter time in HH:MM format like 01:15 in 12-hour format\n");
gets(time);
The user is prompted to input the time in the format HH:MM, representing hours and minutes in a 12-hour clock format. gets() is used to read the input string.
- Extract Hours and Minutes:
while (time[k] != '\0')
{
j = 0;
while (time[k] != ':' && time[k] != '\0')
{
substr[i][j] = time[k];
k++;
j++;
}
substr[i][j] = '\0';
i++;
if (time[k] != '\0')
{
k++;
}
}
The code parses the input string to extract hours and minutes by splitting at the colon ‘:‘.
- Convert to Integers:
int len = i;
hour = atoi(substr[0]);
min = atoi(substr[1]);
Converts the extracted substrings to integers using atoi().
- Check for Valid Time:
if (hour < 1 || hour > 12 || min < 0 || min > 59)
{
printf("Enter Valid Time\n");
exit(0);
}
Checks if the entered hour and minute values are within valid ranges. If not, prints an error message and exits the program.
- Determine Time Phrases:
else
{
s[] array based on the minutes and hours.
}
Based on the entered time, sets flags in the s[] array to indicate which time-related phrases to include in the output.
- Print Result:
for (i = 0; i < 21; i++)
{
if (s[i] == 1)
{
printf("%s ", temp[i]);
}
}
printf("\n");
Prints the time-related phrases corresponding to the set flags in the s[ ] array.
- Close Main Function:
}
Closes the main function.