C PROGRAM
Program to convert 24 hour time into 12 hour time |C Program|
Introduction
Time is a fundamental aspect of our lives, and in programming, we often encounter scenarios where we need to manipulate and represent time in different formats. In this blog post, we'll explore a C program that converts time from the 24-hour format to the more common 12-hour format. We'll analyze the code step by step and understand how it accomplishes this conversion.
Code
// Program to convert 24 hour time into 12 hour time
#include<stdio.h>
#include<conio.h>
struct time
{
int h;
int m;
int s;
};
void main()
{
struct time tt;
clrscr();
printf("enter the hour,min,sec:");
scanf("%d%d%d",&tt.h,&tt.m,&tt.s);
if(tt.h>12)
{
tt.h=tt.h-12;
}
printf("the 12hr time for given time is:\n");
printf("%d:%d:%d",tt.h,tt.m,tt.s);
getch();
}
Output
// Output
// Output
enter the hour,min,sec:16 30 12
the 12hr time for given time is:
4:30:12
Explanation
1. Header Files: The program includes standard input/output and console functions using and .
Conclusion
This C program demonstrates a simple yet practical application of structures and conditional statements to convert time from the 24-hour format to the 12-hour format. Understanding such programs not only enhances programming skills but also provides insights into real-world applications where time representations vary based on conventions. The ability to manipulate time formats is essential in various programming scenarios, making this example valuable for learners and developers alike.
Post a Comment
0 Comments