编写一个使用 if 和 elseif 语句进行时间转换的 C 语言程序
问题
如何使用 C 语言编程将时间从 24 小时制转换为 12 小时制?
解决方案
从用户读取时间值(运行时)。它必须从 24 小时制转换为 12 小时制。
算法
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:
程序
#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;
}输出
enter the time in 24 hour format:22:37 The 12 hr format time:10:37 PM
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP