编写程序,输入一个字符ch,输出字符的类型,即字母(alpha)、数字(numeric)或其他字符(other)。
#include,stdio.h”
main()
{
char ch;
ch=getchar();
if(ch>=65&&ch<=90||ch>=97&&ch<=122)
{
printf(“\nch=%c”,ch);
printf(“\nThis is an alpha!”);
}
else
if(ch>=48&&ch<=57)
{
printf(“\nch=%c”,ch);
printf(“\nThis is a numeric!”);
}
else
printf(“\nThis is other!”);
}
编写程序,输入一个正整数,判断该数是奇数还是偶数,并输出结果。
main()
{
int m;
printf(“\nPlease input the integer(>0):”);
scanf(“%d”,&m);
printf(“\nThe integer is %d”,m);
if(m%2==0)
printf(“\nThis is a even!”);
else
printf(“\nThis is a odd!”);
}
3,有一函数:
编写一个程序,用scanf函数输入x的值,输出y值。注意表达式的书写方法。
运行程序,输入x的值(分别用上述4种情况),检查输出的y值是否正确。
#include,math.h”
main()
{
float x,y;
printf(“\nPlease input the x:”);
scanf(“%f”,&x);
if(x<-1)
y=pow(x,3)-1;
else
if(x<=1)
y=-x*x+1;
else
if(x<=10)
y=3*x+5;
else
y=5*x+3*log(2*x*x-1)-13;
printf(“\ny=%f”,y);
}
4,编写程序,输入年号,判断并输出该年是否闰年。
main()
{
int year;
printf(“\nPlease input the year:”);
scanf(“%d”,&year);
if(year%4==0&&year%100!=0||year%400==0)
printf(“\n%d is a leap year!”,year);
else
printf(“\n%d is not a leap year!”,year);
}
6,从键盘输入三个数,代表三条线段的长度。请编写程序,判断这三条线段组成的三角形是什么类型(不等边、等腰、等边或不能构成三角形)。
main()
{
int a,b,c;
printf(“\nPlease input three numbers:”);
scanf(“%d%d%d”,&a,&b,&c);
if(a+b>c&&b+c>a&&a+c>b)
{
if(a==b&&b==c)
printf(“\nThis is an equilateral triangle!”);
else
if(a==b||b==c||a==c)
printf(“\nThis is an isosceles triangle!”);
else
printf(“\nThis is an unequilateral triangle!”);
}
else
printf(“\nThis is not a triangle!”);
}
7,简单选择界面的编程。从键盘输入整数,输出不同的字符串:
输入1,输出Good morning;
输入2,输出Good afternoon;
输入3,输出Good evening;
输入4,输出Good night;
输入其他数字,输出Bye bye。
main()
{
int n;
printf("\nPlease input a number:");
scanf("%d",&n);
switch(n)
{
case 1:printf("Good morning");break;
case 2:printf("Good afternoon");break;
case 3:printf("Good evening");break;
case 4:printf("Good night");break;
default:printf("Bye bye");
}
}
8.已知从银行贷款月利率为:期限一年,为0.90%;期限2年,为1%;期限3年,为1.11%;三年以上为1.2%。从键盘输入贷款金额和期限,计算到期后应归还银行本金和利息合计为多少钱。
#include <math.h>
main()
{
int year;
float base,rate,sum;
clrscr();
printf("\nEnter the base and the year:");
scanf("%f%d",&base,&year);
switch(year)
{
case 1,rate=0.009;break;
case 2,rate=0.01;break;
case 3,rate=0.0111;break;
default,rate=0.012;
}
sum=base*pow(1+rate,12*year);
printf("sum is %.2f\n",sum);
}
9,输入一个不多于5位的正整数,要求:① 求出它是几位数;② 分别打印出每一位数字;③ 按逆序打印出各位数字。
要准备以下测试数据:
1位正整数
2位正整数
3位正整数
4位正整数
5位正整数除此之外,程序还应当对不合法的输入作必要的处理,如负数或超过5位的正整数。
main()
{
long m;
int a,b,c,d,e;
printf(“\nPlease input a integer(m>0&&m<=99999):”);
scanf(“%ld”,&m);
if(m>=10000&&m<=99999)
{
printf(“\nThis is a 5 integer!”);
a=m/10000;
b=m/1000-a*10;
c=m/100-a*100-b*10;
d=m/10-a*1000-b*100-c*10;
e=m%10000;
printf(“\nThe integer is %d %d %d %d %d!”,a,b,c,d,e);
printf(“\nThe inverse is %d %d %d %d %d!”,e,d,c,b,a);
}
else
if(m>=1000&&m<=9999)
{
printf(“\nThis is a 4 integer!”);
a=m/1000;
b=m/100-a*10;
c=m/10-a*100-b*10;
d=m%1000;
printf(“\nThe integer is %d %d %d %d!”,a,b,c,d);
printf(“\nThe inverse is %d %d %d %d!”,d,c,b,a);
}
else
if(m>=100&&m<=999)
{
printf(“\nThis is a 3 integer!”);
a=m/100;
b=m/10-a*10;
c=m%100;
printf(“\nThe integer is %d %d %d!”,a,b,c);
printf(“\nThe inverse is %d %d %d!”,c,b,a);
}
else
if(m>=10&&m<=99)
{
printf(“\nThis is a 2 integer!”);
a=m/10;
b=m%100;
printf(“\nThe integer is %d %d!”,a,b);
printf(“\nThe inverse is %d %d!”,b,a);
}
else
if(m>=1&&m<=9)
{
printf(“\nThis is a 1 integer!”);
printf(“\nThe integer is %ld”,m);
}
else
printf(“\nNot the effect integer!”);
}
#include,stdio.h”
main()
{
char ch;
ch=getchar();
if(ch>=65&&ch<=90||ch>=97&&ch<=122)
{
printf(“\nch=%c”,ch);
printf(“\nThis is an alpha!”);
}
else
if(ch>=48&&ch<=57)
{
printf(“\nch=%c”,ch);
printf(“\nThis is a numeric!”);
}
else
printf(“\nThis is other!”);
}
编写程序,输入一个正整数,判断该数是奇数还是偶数,并输出结果。
main()
{
int m;
printf(“\nPlease input the integer(>0):”);
scanf(“%d”,&m);
printf(“\nThe integer is %d”,m);
if(m%2==0)
printf(“\nThis is a even!”);
else
printf(“\nThis is a odd!”);
}
3,有一函数:
编写一个程序,用scanf函数输入x的值,输出y值。注意表达式的书写方法。
运行程序,输入x的值(分别用上述4种情况),检查输出的y值是否正确。
#include,math.h”
main()
{
float x,y;
printf(“\nPlease input the x:”);
scanf(“%f”,&x);
if(x<-1)
y=pow(x,3)-1;
else
if(x<=1)
y=-x*x+1;
else
if(x<=10)
y=3*x+5;
else
y=5*x+3*log(2*x*x-1)-13;
printf(“\ny=%f”,y);
}
4,编写程序,输入年号,判断并输出该年是否闰年。
main()
{
int year;
printf(“\nPlease input the year:”);
scanf(“%d”,&year);
if(year%4==0&&year%100!=0||year%400==0)
printf(“\n%d is a leap year!”,year);
else
printf(“\n%d is not a leap year!”,year);
}
6,从键盘输入三个数,代表三条线段的长度。请编写程序,判断这三条线段组成的三角形是什么类型(不等边、等腰、等边或不能构成三角形)。
main()
{
int a,b,c;
printf(“\nPlease input three numbers:”);
scanf(“%d%d%d”,&a,&b,&c);
if(a+b>c&&b+c>a&&a+c>b)
{
if(a==b&&b==c)
printf(“\nThis is an equilateral triangle!”);
else
if(a==b||b==c||a==c)
printf(“\nThis is an isosceles triangle!”);
else
printf(“\nThis is an unequilateral triangle!”);
}
else
printf(“\nThis is not a triangle!”);
}
7,简单选择界面的编程。从键盘输入整数,输出不同的字符串:
输入1,输出Good morning;
输入2,输出Good afternoon;
输入3,输出Good evening;
输入4,输出Good night;
输入其他数字,输出Bye bye。
main()
{
int n;
printf("\nPlease input a number:");
scanf("%d",&n);
switch(n)
{
case 1:printf("Good morning");break;
case 2:printf("Good afternoon");break;
case 3:printf("Good evening");break;
case 4:printf("Good night");break;
default:printf("Bye bye");
}
}
8.已知从银行贷款月利率为:期限一年,为0.90%;期限2年,为1%;期限3年,为1.11%;三年以上为1.2%。从键盘输入贷款金额和期限,计算到期后应归还银行本金和利息合计为多少钱。
#include <math.h>
main()
{
int year;
float base,rate,sum;
clrscr();
printf("\nEnter the base and the year:");
scanf("%f%d",&base,&year);
switch(year)
{
case 1,rate=0.009;break;
case 2,rate=0.01;break;
case 3,rate=0.0111;break;
default,rate=0.012;
}
sum=base*pow(1+rate,12*year);
printf("sum is %.2f\n",sum);
}
9,输入一个不多于5位的正整数,要求:① 求出它是几位数;② 分别打印出每一位数字;③ 按逆序打印出各位数字。
要准备以下测试数据:
1位正整数
2位正整数
3位正整数
4位正整数
5位正整数除此之外,程序还应当对不合法的输入作必要的处理,如负数或超过5位的正整数。
main()
{
long m;
int a,b,c,d,e;
printf(“\nPlease input a integer(m>0&&m<=99999):”);
scanf(“%ld”,&m);
if(m>=10000&&m<=99999)
{
printf(“\nThis is a 5 integer!”);
a=m/10000;
b=m/1000-a*10;
c=m/100-a*100-b*10;
d=m/10-a*1000-b*100-c*10;
e=m%10000;
printf(“\nThe integer is %d %d %d %d %d!”,a,b,c,d,e);
printf(“\nThe inverse is %d %d %d %d %d!”,e,d,c,b,a);
}
else
if(m>=1000&&m<=9999)
{
printf(“\nThis is a 4 integer!”);
a=m/1000;
b=m/100-a*10;
c=m/10-a*100-b*10;
d=m%1000;
printf(“\nThe integer is %d %d %d %d!”,a,b,c,d);
printf(“\nThe inverse is %d %d %d %d!”,d,c,b,a);
}
else
if(m>=100&&m<=999)
{
printf(“\nThis is a 3 integer!”);
a=m/100;
b=m/10-a*10;
c=m%100;
printf(“\nThe integer is %d %d %d!”,a,b,c);
printf(“\nThe inverse is %d %d %d!”,c,b,a);
}
else
if(m>=10&&m<=99)
{
printf(“\nThis is a 2 integer!”);
a=m/10;
b=m%100;
printf(“\nThe integer is %d %d!”,a,b);
printf(“\nThe inverse is %d %d!”,b,a);
}
else
if(m>=1&&m<=9)
{
printf(“\nThis is a 1 integer!”);
printf(“\nThe integer is %ld”,m);
}
else
printf(“\nNot the effect integer!”);
}