1.编写程序:从键盘上接受一个字母,若是大写字母按原样输出,若是小写字母则将 其转化为大写字母输出。
#include<stdio.h>
#include<stdlib.h>
int main()
{
char ch;
printf("请输入一个字母:");
ch=getchar();
putchar(toupper(ch));
system("pause");
return 0;
}
2.if 语句编程序求解下列式子,输入 x 后按下式计算 y 值并输出。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x,y;
printf("请输入x:\n");
scanf("%d",&x);
if(0<=x<=8){
y=x+2*x*x+10;
}else{
y=x-3*x*x*x-9;
}
printf("y的值为:");
printf("%d",y);
system("pause");
return 0;
}
3. 用 if…else 语句编程实现:输入 一个学生成绩(百分制),对成绩进行等级划分: 当成绩大于等于 90 分时输出“优秀”;//当成绩大于等于 80 分且小于 90 分时输出“良 好”;//当成绩大于等于 70 分且小于 80 分时输出“中等”;//当成绩大于等于 60 分且小于 70 分时输出“及格”;//当成绩小于 60 分时为“不及格”
#include<stdio.h>
#include<stdlib.h>
int main(){
int a;
printf("请输入成绩:");
scanf("%f", &a);
if(a>=90)
printf("为优秀\n",a) ;
else if (a >= 80)
printf("为良好\n",a);
else if (a >= 70)
printf("为中等\n",a);
else if(a>=60)
printf("为及格\n");
system("pause");
return 0;
}
4.4.编写程序,计算数学表达式的值。编程要求如下: (1)x 的值从键盘输人。 (2)分别计算表达式 sinx.ln(x+1).ex.|cosx|的值,然后计算整个表达式的值。
(3)对被调用的标准库函数,必须加注释说明其功能
#include <stdio.h>
#include <math.h>
int main()
{
float x,y,a,b,c,d;
printf("请输入x的值:");
scanf("%f",&x);
a=sin(x);
b=log(x+1);
c=exp(x); //e的x次方
d=fabs(cos(x)); //绝对值
y=(a+b)/(c+d);
printf("%f",y);
getchar();
return 0;
}
5.编写一个程序,确定一个数的位数:
Enter a number:374 The number 374 has 3 digits
#include<stdio.h>
#include<stdlib.h>
int main(void){
int x,y;
int z;
printf("Enter a number:");
scanf("%d",&x);
y=x;
for(z=0;x>1;z++)
{
x=x/10;
}
printf("The number %d has %d digits\n",y,z);
system("pause");
return 0;
}
6. 编写一个程序,要求用户输入 24 小时制的时间,然后显示 12 小时制的格式 Enter a 24-hour time:21:11 Equivalent 12-hour time:9:11PM 注意不要把 12:00 显示成 0:00
#include <stdio.h>
#include<stdlib.h>
int main(void) {
int a,b;
printf("Enter a 24-hour time:");
scanf("%d:%d",&a,&b);
if (a >= 12) {
if (12 == a) printf("Equivalent 12-hour time:%d:%.2d PM",a-12,b);
else {
a -= 12;
printf("Equivalent 12-hour time:%d:%.2d PM",a,b);
}
}
else {
printf("Equivalent 12-hour time:%d:%.2d AM",a,b);
}
system("pause");
return 0;
}
#7
#include<stdio.h>
#include<stdlib.h>
int main(void){
float a,b;
printf("Enter value of trade:");
scanf("%f",&a);
if(a<2500)
b=30+a*0.017;
else if(a<6250)
b=56+a*0.0066;
else if(a<20000)
b=76+a*0.0034;
else if(a<50000)
b=100+a*0.0022;
else if(500000)
b=155+a*0.0011;
else
b=255+a*0.0009;
printf("Commission:$%.2f",b);
system("pause");
return 0;
}
8 编写一个程序,要求用户输入风速(海里/每小时),然后显示相 应的描述
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a;
scanf ("%d", &a);
if (a < 1)
printf("Calm");
else if (a <= 3)
printf("Light air");
else if (a <= 27)
printf("Breeze");
else if (a <= 47)
printf("Gale");
else if (a <= 63)
printf("Storm");
else
printf("Hurricane");
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
float value,broke;//
printf("Enter value of trade:");
scanf("%f",&value);
if (value<2500)
broke=30+value*0.017;
else if (value<6250)
broke=56+value*0.0066;
else if (value<20000)
broke=76+value*0.0034;
else if (value<50000)
broke=100+value*0.0022;
else if (value<500000)
broke=155+value*0.0011;
else
broke=255+value*0.0009;
printf("Commission:$%.2f\n",broke);
system("pause");
return 0;
}
10. 编写一个程序,从用户输入的 4 个整数中找到最大值和最小值 Enter four integers:21 43 10 35 Largest:43 Smallest:10
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i1,i2,i3,i4,max,min;
printf("Enter four integers:");
scanf("%d %d %d %d",&i1,&i2,&i3,&i4);
//求最大值
if(i1>i2){
max=i1;
}
else{
max=i2;
}
if(i3>max){
max=i3;
}
if(i4>max){
max=i4;
}
//求最小值
if(i1<i2){
min=i1;
}
else{
min=i2;
}
if(i3<min){
min=i3;
}
if(i4<min){
min=i4;
}
printf("Largest:%d\n",max);
printf("Smallest:%d\n",min);
system("pause");
return 0;
}
Enter a 24-hour time:13:15 Closest departure time is 12:47pm,arriving at 3:00 pm (提示:把输入用从午夜开始的分钟数表示。将这个时间与表格里也用从午夜开始的 分钟数表示的起飞时间相比。例如,13 点 15 分从午夜开始是 13*60+15=795 分钟,与下午 12 点 47 分从午夜开始是 767 分钟最接近)
#include<stdio.h>
#include<stdlib.h>
int main(void){
printf("Enter a 24-hour time[23:56]:");
int hour,minute;
scanf("%d:%d",&hour,&minute);
int total_minute=60*hour+minute;
if (total_minute<615/2){
printf("Closet departure time is 9:45 PM,arriving at 11:58 PM");
}
else if(total_minute<(8*60+9*60+43)/2){
printf("Closet departure time is 8:00 AM,arriving at 10:16 AM");
}
else if(total_minute<(9*60+43+11*60+19)/2){
printf("Closet departure time is 9:43 AM,arriving at 11:52 AM");
}
else if(total_minute<(11*60+19+12*60+47)/2){
printf("Closet departure time is 11:19 AM,arriving at 1:31 PM");
}
else if(total_minute<(12*60+47+14*60)/2){
printf("Closet departure time is 12:47 PM,arriving at 3:00 PM");
}
else if(total_minute<(15*60+45+19*60)/2){
printf("Closet departure time is 3:45 PM,arriving at 5:55 PM");
}
else if(total_minute<(19*60+21*60+45)/2){
printf("Closet departure time is 7:00 PM,arriving at 9:20 PM");
}
else printf("Closet departure time is 9:45 PM,arriving at 11:58 PM");
system("pause");
return 0;
}
12. 编写一个程序,提示用户输入两个日期,然后显示哪一个日期更早: 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
#include<stdio.h>
#include<stdlib.h>
int main()
{
int mm,mm1,dd,dd1,yy1,yy;
printf("Enter first date (mm/dd/yy):");
scanf("%d/%d/%d",&mm,&dd,&yy);
printf("Enter second date (mm/dd/yy):");
scanf("%d/%d/%d",&mm1,&dd1,&yy1);
if(yy<yy1){
printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",mm1,dd1,yy1,mm,dd,yy);
}
else if(yy1>yy){
printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",mm,dd,yy,mm1,dd1,yy1);
}
else{
if(mm1<mm){
printf("%d/%d/%.2d is earlier than %d/%d/%2.d\n",mm1,dd1,yy1,mm,dd,yy);
}
else if (mm1>mm){
printf("%d/%d/%2.d is earlier than %d/%d/%2.d\n",mm,dd,yy,mm1,dd1,yy1);
}
else{
if(dd1<dd){
printf("%d/%d/%.2d is earlier than %d/%d/%2.d\n",mm1,dd1,yy1,mm,dd,yy);
}
else if(dd1>dd){
printf("%d/%d/%.2d is earlier than %d/%d/%2.d\n",mm,dd,yy,mm1,dd1,yy1);
}
else{
printf("The two dates are the same.\n");
}
}
}
system("pause");
return 0;
}
13. 利用 switch 语句编写一个程序,把用数字表示的成绩转化为字母表示的等级。 Enter numerical grade:84 Letter grade:8 使用下面的等级评定规则:A 为 90-100,B 为 80-89,C 为 70 至 79,D 为 60 之 69,F 为 0 至 59。如果成绩高于 100 或低于 0 显示出错信息。提示:把成绩拆分为 2 个数字,然 后使用 switch 语句判定十位上的数字
#include<stdio.h>
#include<stdlib.h>
int main()
{
int scort;
printf("Enter numerical grade:");
scanf("%d",&scort);
if(scort>100 || scort<0){
printf("Lllegal\n");
}
else{
printf("Letter grade:");
switch (scort/10){
case 10:
case 9:
printf("A\n");
break;
case 8:
printf("B\n");
break;
case 7:
printf("C\n");
break;
case 6:
printf("D\n");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
printf("F\n");
break;
}
}
system("pause");
return 0;
}
14. 编写写一个程序,要求用户输入一个两位数,然后显示该数的英文单词: Enter a two-digit number:45 You entered the number forty-five 提示:把数分解为 2 个数字。用一个 switch 语句显示第一位数字对应的单词 (“twenty”、”thirty”等),用第 2 个 switch 语句显示第 2 位数字对应的单词。不要忘记 11-19 需要特殊处理
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int a,b;
printf("Enter a two-dight number:");
scanf("%1d%1d",&a,&b);
printf("You entered the number ");
if(a!=1){
switch (a)
{
case 2:
printf("twenty-");break;
case 3:
printf("thirty-");break;
case 4:
printf("forty-");break;
case 5:
printf("fifty-");break;
case 6:
printf("sixty-");break;
case 7:
printf("seventy-");break;
case 8:
printf("eighty-");break;
case 9:
printf("ninty-");break;
}
switch (b)
{
case 1:
printf("one.\n");break;
case 2:
printf("two.\n");break;
case 3:
printf("three.\n");break;
case 4:
printf("four.\n");break;
case 5:
printf("five.\n");break;
case 6:
printf("six.\n");break;
case 7:
printf("seven.\n");break;
case 8:
printf("eight.\n");break;
case 9:
printf("nine.\n");break;
}
}
else{
switch (b){
case 1:
printf("eleven.\n");break;
case 2:
printf("twelve.\n");break;
case 3:
printf("thirteen.\n");break;
case 4:
printf("fourteen.\n");break;
case 5:
printf("fifteen.\n");break;
case 6:
printf("sixteen.\n");break;
case 7:
printf("seventeen.\n");break;
case 8:
printf("eighteen.\n");break;
case 9:
printf("ninteen.\n");break;
}
}
system("pause");
return 0;
}
来源:oschina
链接:https://my.oschina.net/u/4741313/blog/4836414