1、编写一个程序,要求用户输入两个数字。在程序使用一个或多个scanf()函数调用接收这些数字之后,用程序检查这些数字。如果第一个键入的数字大于第二个数字则输出消息“The first number is greater than the second”,否则输出消息“The first number is not greater than the second”.
#include<stdio.h>
int main()
{
float a,b;
scanf("%f %f",&a,&b);
if(a>b) printf("The first number is greater than the second\n");
else printf("The first number is not greater than the second\n");
return 0;
}
2、输入3个数a,b,c,要求按由小到大的顺序输出。(注意:以下代码为C++)
#include<iostream>
#include<cstdlib>
#include<cstdio>
int main()
{
using namespace std;
float a[3];
int i,j,k;
float temp;
scanf("%f %f %f",&a[0],&a[1],&a[2]);
for(i=0;i<3;i++)
{
k=i;
for(j=i;j<3;j++)
{
if(a[j]<a[k]) k=j;
}
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;
return 0;
}
3、编写一个程序,用scanf()函数接收一个字符并确定这个字符是否是一个小写字母。如果输入的字符是一个小写字母,显示消息“The character just entered is a lowercase letter”,如果输入的字符不是一个小写字母,显示消息“The character just entered is not a lowercase letter”。
#include<stdio.h>
int main()
{
char a;
scanf("%c",&a);
if(a>='a'&&a<='z') printf("The character just entered is a lowercase letter\n");
else printf("The character just entered is not a lowercase letter\n");
return 0;
}
4、有一个函数 X (X<1)
y= 2X-1 (1≤X<10)
3X-11 (X≥10)
要求输入x值,输出y值。(注意:以下代码为C++)
#include<iostream>
#include<cstdlib>
#include<stdio.h>
int main()
{
using namespace std;
float x,y;
scanf("%f",&x);
if(x<1) y=x;
else if(x>=1&&x<=10) y=2*x-1;
else y=3*x-11;
cout<<y<<endl;
return 0;
}
5、输入百分制成绩,要求输出成绩等级’A’,’B’,’C’,’D’,’E’。90分以上为’A’,80-89分为’B’,70-79分为’C’, 60-69分为’D’,60分以下为’E’
#include<stdio.h>
int main()
{
int a,b;
scanf("%d",&a);
b=a/10;
switch(b)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
printf("E\n");
break;
case 6:
printf("D\n");
break;
case 7:
printf("C\n");
break;
case 8:
printf("B\n");
break;
case 9:
case 10:
printf("A\n");
break;
}
return 0;
}