21.htm 01. 从键盘输入一个字符,如果是大写字母,就转换成小写;如果是 小写字母,就转换成大写,如果是其他字符原样保持并将结果输出。 #include <stdio.h> void main() { char c; scanf("%c",&c); if(c>='A'&&c<='Z') c=c+32; else if(c>='a'&&c<='z') c=c-32; printf("%c",c); } 02. 从键盘输入一个数,判断其是否是 5 的倍数而不是 7 的倍数。如 果是,输出 Yes,否则输出 No。 #include <stdio.h> void main() { int a; scanf("%d",&a); if(a%5==0&&a%7!=0) printf("yes"); else printf("no"); } 03. 从键盘输入一个 4 位正整数,求其逆序数,并输出。例如:若输 入 1234,则输出应该是 4321。 #include <stdio.h> void main() { int n,g,s,b,q,m; scanf("%d",&n); q=n/1000; b=n/100%10; s=n/10%10; g=n%10; m=g*1000+s*100+b*10+q; printf("%d\n",m); } 22.htm 01. 从键盘输入一个字符,如果是字母,就输出其对应的 ASCII 码; 如果是数字字符,就转换成对应整数并输出。 #include <stdio.h> void main() { char c; scanf("%c",&c); if(c>='a'&&c<='z'|| c>='A'&&c<='Z') printf("ASCII:%d",c); else if(c>='0'&&c<='9') printf("数字: %d",c-'0'); } 02. 从键盘输入一个数,判断其是否能同时被 3 和 5 整除。如果是, 输出 Yes,否则输出 No。 #include <stdio.h> void main() { int a; scanf("%d",&a); if(a%3==0&&a%5==0) printf("yes"); else printf("no"); } 03. 从键盘输入一个 4 位正整数,求其各位数字之积,并输出。例如: 若输入 2523,则输出应该是 60。 #include <stdio.h> void main() { int n,g,s,b,q,m; scanf("%d",&n); q=n/1000; b=n/100%10; s=n/10%10; g=n%10; m=g*s*b*q; printf("\n%d\n",m); } 23.htm 01. 根据以下函数关系编写一个程序,对输入的每个 x 值,计算出 y 的值并输出。 #include <stdio.h> void main() { int x,y; scanf(“%d”,&x); if (x>-10 && x<0 ) y=x+10; else if (x<10) y=2*x; else y=x*x; printf(“x=%d,y=%d\n ”,x,y); } 02. 从键盘输入一个数,判断其是否是 5 的倍数而不是 7 的倍数。如 果是,输出 Yes,否则输出 No。 #include <stdio.h> void main() { int a; scanf("%d",&a); if(a%5==0&&a%7!=0) printf("yes"); else printf("no"); } 03. 从键盘输入一个五位整数,判断它是不是对称数,并输出判断结 果。如 43234 就是对称数。 #include <stdio.h> void main() { int n,g,s,b,q,w; scanf("%d",&n); w=n/10000; q=n/1000%10; b=n/100%10; s=n/10%10; g=n%10; if(w==g && q==s) printf("%d 是对称数\n",n); else printf("%d 不是对称数\n",n); } 24.htm 01. 编一个 C 程序,从键盘上输入一个字符: 若该字符是数字字符, 则把它转换为对应的整数并输出; 若该字符是大写字母,则转换成小 写并输出; 若该字符是小写字母,则转换为大写并输出; 若该字符是其他字符,则不进行任何操作。 #include <stdio.h> void main() { char c; scanf("%c",&c); if(c>='0'&&c<='9') printf("%d\n",c'0'); else if(c>='A'&&c<='Z') printf("%c\n",c+32); else if(c>='a'&&c<='z') printf("%c\n",c-32); } 02. 输入三个整数,按从大到小的顺序输出。 #include <stdio.h> void main() { int a,b,c,t; scanf("%d%d%d",&a,&b,&c); if (a<b) { t=a;a=b;b=t; } if (a<c) { t=a;a=c;c=t; } if (b<c) { t=b;b=c;c=t; } printf(“%d,%d ,%d ",a,b,c); } 03. 给 出 一 个 百 分 制 的 成 绩 , 要 求 输 出 成 绩 等 级 “ A” 、 “ B” , “ C” , “ D” , “ E” 。 90 分 以 上 的 为 : “ A” 级 , 80~89 分的为“B”,70~79 分的为“C”,60~69 分的为“D”,60 分以下的为“E”。 #include <stdio.h> void main() { int grade; scanf(“%d”,&grade); switch(grade/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; default:printf(”E\n”);break; } } 25.htm 01. 有一分段函数: 1 ( x < -1 ) y = 2 x + 9 ( -1 ≤ x ≤ 1 ) 5 x -3 ( x > 1 ) 从键盘输入 x 的值,输出 y 的值。 #include <stdio.h> void main() { int x,y; scanf(“%d”,&x); if (x<-1) y=1; else if (x<=1) y=2*x+9; else y=5*x-3; printf(“x=%d,y=%d\n ”,x,y); } 02. 编写一个 C 语言程序,要求从键盘输入三个整数,并根据对三个数 的比较显示如下信息: ① 如果三个数都不相等则显示 0; ② 如果三个数中有二个数相等则显示 1; ③ 如果三个数都相等,则显示 2 #include <stdio.h> void main() { int a,b,c; scanf(“%d%d%d”,&a,&b,&c); if (a==b && b==c) printf(“2\n ”); else if (a==b || b==c || a==c ) printf(“1\n ”); else printf(“0\n ”); } 03. 从键盘输入一个数,判断它是否同时能被 5 和 7 整除,如果能输出 “YES”,否则输出“NO”。 #include <stdio.h> void main() { int a; scanf("%d",&a); if(a%5==0&&a%7==0) printf("yes"); else printf("no"); } 26.htm 01.输入三角形三边长,判定是否能够组成三角形,并输出判定结果。 能够组成三角形的条件是:任意两边之和大于第三边(三种情况) #include <stdio.h> void main() { int a,b,c; scanf(“%d%d%d”,&a,&b,&c); if (a+b>c && b+c>a && a+c>b) printf(“YES\n ”); else printf(“NO\n ”); } 02. 从键盘输入一个四位正整数,输出其逆序数,并判断是否是对称数, 如果是输出“YES”,否则输出“NO”。 提示:如果输入 4253,先求出其每位上的数字,再重新组成其逆 序数 3524。 如果输入 4224,求出其逆序数是 4224,则其是对称数。 #include <stdio.h> void main() { int n,g,s,b,q,m; scanf("%d",&n); q=n/1000; b=n/100%10; s=n/10%10; g=n%10; m=g*1000+s*100+b*10+q; if (n==m) printf(“YES\n ”); if (n==m) printf(“YES\n ”); else printf(“NO\n ”); } 03. 从键盘输入三个数,求其中最小者并输出。 #include <stdio.h> void main() { int a,b,c,min; scanf("%d%d%d",&a,&b,&c); if (a<b) min=a; else min=b; if (min>c) min=c; printf(“min=%d ",min); }
C Language Program - Branch Structure Topics and Answers
Tips: Current document can only be previewed at most page8,If the total number of pages in the document exceeds page 8,please download the document。
Uploaded by admin on 2019-10-15 12:01:26