《C程序设计语言现代方法》第5章 编程题

时间:2021-06-08 14:41:11

1 编写一个程序,确定一个数的位数。

 #include <stdio.h>

 int main()
{
int a, cnt = ; while (scanf("%1d", &a) == )
{
cnt++;
}
printf("%d\n", cnt); return ;
}

《C程序设计语言现代方法》第5章 编程题

注:在Windows下,输入完毕后先按Enter键,再按CTRL+Z键,最后再按Enter键,即可结束输入。在Linux下,输入完毕后按CTRL+D键即可结束输入。

2 编写一个程序,要求用户输入24小时制的时间,然后显示12小时制的时间格式:

Enter a 24-hour time: 21:11

Equivalent 12-hour time: 9:11 PM

注意不要把12:00显示成0:00

 #include <stdio.h>

 int main()
{
int h, m; printf("Enter a 24-hour time: ");
scanf("%d:%d", &h, &m);
if (h > || h < || m > || m < )
printf("Wrong time.\n");
else
{
printf("Equivalent 12-hour time: ");
if (h > )
printf("%d:%d PM\n", h%, m);
else
printf("%d:%d AM\n", h, m);
} return ;
}

运行结果如下:

《C程序设计语言现代方法》第5章 编程题

7 编写一个程序,从用户输入的4个整数中找出最大值和最小值。

Enter four integers: 21 43 10 45

Largest: 43

Smallest: 10

要求尽可能少用if语句。

9 编写一个程序,提示用户输入两个日期,然后显示哪一个日期更早:

Enter first date (mm/dd/yy) : 3/6/08

Enter second date (mm/dd/yy) : 5/17/07

5/17/07 is earlier than 3/6/08

11 编写一个程序,要求用户输入一个两位数,然后显示该数的英文单词:

Enter a two-digit number : 45

You entered the number forty-five