Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a C program for time conversions using if and elseif statements
Problem
How to convert the time from 24 hr format to 12 hr format using C Programming language?
Solution
Read the time value from the user (at run time). It has to be converted into 12 hr format from 24 hr.
Algorithm
Start: Step 1: Enter time in 24 hr format Step 2: check the condition i. If(hour==0) Print min Ii. Elseif(hour<12) Print hour,min iii. Elseif(hour==12 Print hour,min iv. Else Print hour % 12,min Stop:
Program
#include<stdio.h>
int main(){
int hr,min;
printf("enter the time in 24 hour format:");
scanf("%d:%d",&hr,&min);
printf("The 12 hr format time:");
if(hr==0){
printf("12:%.2d AM
",min);
}
else if(hr<12){
printf("%d:%.2d AM
",hr,min);
}
else if(hr==12){
printf("%d:%.2d PM
",hr,min);
}
else
printf("%d:%.2d PM
",hr % 12,min);
return 0;
}
Output
enter the time in 24 hour format:22:37 The 12 hr format time:10:37 PM
Advertisements