十三、编写输出如下分段函数值的程序,要求x的值从键盘输入。
1 #include <stdio.h> 2 3 void main(void) 4 { 5 float x, y; 6 7 printf("Input x:\n"); 8 scanf("%f", &x); 9 if(x <= 0) 10 y = x + 1; 11 else if(x <= 1) 12 y = 1; 13 else 14 y = x; 15 printf("x = %f, y = %f\n", x, y); 16 }
结果:
1.Input x:
3
x = 3.000000, y = 3.000000
2.Input x:
-1
x = -1.000000, y = 0.000000
3.Input x:
0.5
x = 0.5000000, y = 1.000000
十四、输入三个整数,输出最大数。
1 #include <stdio.h> 2 3 void main(void) 4 { 5 int a, b, c, max; 6 7 printf("Input a, b, c: "); 8 scanf("%d%d%d", &a, &b, &c); 9 max = a; 10 if(max < b) 11 max = b; 12 if(max < c) 13 max = c; 14 printf("a = %d, b = %d, c = %d, max = %d\n", a, b, c, max); 15 }
十五、输入三个不同的整数,分别存放在a、b、c中,再把这三个数按从小到大的顺序重新放入a、b、c后输出。
1 #include <stdio.h> 2 3 void main(void) 4 { 5 int a, b, c, temp; 6 7 printf("Input a, b, c:\n"); 8 scanf("%d%d%d", &a, &b, &c); 9 printf("Before : a = %d, b = %d, c = %d\n", a, b, c); 10 if(a > b) 11 { temp = a; a = b; b = temp;} 12 if(b > c) 13 { temp = b; a = c; c = temp;} 14 if(a > b) 15 { temp = a; a = b; b = temp;} 16 printf("After : a = %d, b = %d, c = %d\n", a, b, c); 17 }
(1)if(a > b) {temp = a; a = b; b = temp;} //是一条语句
(2)if(a > b) {temp = a; a = b; b = temp;}; //是两条语句
(3)if(a > b); {temp = a; a = b; b = temp;}; //是两条语句
十六、输入一个整数,如果是偶数,则输出Even number,如果是奇数,则输出Odd number。
1 #include <stdio.h> 2 3 void main(void) 4 { 5 int a; 6 7 printf("Input a: "); 8 scanf("%d", &a); 9 if(a % 2 == 0) 10 printf("Even number.\n"); 11 else 12 printf("Odd number.\n"); 13 }
时间: 2024-10-24 00:19:01