非计算机专业C语言考试经典编程题汇编
求面积:
求任意圆面积:
#include
#define PI 3.1415926
main ( )
{
float r,s;
printf("请输入圆的半径:\n");
scanf("r=%f",&r);
s=PI*r*r;
printf("该圆的面积为:s=%f\n",s);
}
2、求任意矩形面积:
#include
main ( )
{
float a,b,c;
printf("请输入矩形面积的长和宽:\n");
scanf("a=%f,b=%f",&a,&b);
s=a*b;
printf("该矩形面积的面积为:s=%f\n",s);
}
求任意梯形面积:
#include
main ( )
{
float a,b,c,s;
printf("请输入梯形面积的上底、下底和高:\n");
scanf("a=%f,b=%f,h=%f",&a,&b,&h);
s=1.0/2.0*(a+b)*h;
printf("该梯形的面积为:s=%f\n",s);
}
数值交换:
方法一:
#include
main ( )
{
float x,y,t;
printf("请输入任意两个数字:\n");
scanf("%f,%f",&x,&y);
printf("x=%f,y=%f\n",x,y);
t=y;
y=x;
x=t;
printf("x=%f,y=%f\n",x,y);
}
方法二:
#include
main ( )
{
float x,y,t;
printf("请输入任意两个数字:\n");
scanf("%f,%f",&x,&y);
printf("x=%f,y=%f\n",x,y);
printf("x=%f,y=%f\n",y,x);
}
三、字符变换:
1、大小写字母转换:(见小变大,见大变小)
#include
main ( )
{
char ch;
printf("please input a letter!\n");
scanf("%c",&ch);
if(ch>='A'&&ch<='Z') ch=ch+32;
if(ch>='a'&&ch<='z') ch=ch—32;
printf ("%c\n",ch);
}
字符加密:
例如:输入一个字母,要求输出该字母后第n个字母:
#include
main ( )
{
char ch1,ch2;
printf("请输入任意一个英文字母!\n");
scanf("%c",.&ch1);
ch2=ch1+n;
printf("加密后的字母或符号为:%c\n",ch2);
}
排列顺序:
1、将三个数从小到大排列:
#include
main ( )
{
float a,b,c,t;
printf("请输入任意三个数!\n");
scanf("%f,%f,%f",&a,&b,&c);
if(a>b) {t=a;a=b;b=t;}
if(a>c) {t=a;a=c;c=t;}