篇一:C语言程序经典例子
计算机程序设计基础(C语言)
编程练习题及参考答案
1.输入2个整数,求两数的平方和并输出。
#include <stdio.h>
main()
{ int a ,b,s;
printf("please input a,b:\n");
scanf("%d%d”,&a,&b);
s=a*a+b*b;
printf("the result is %d\n",s);
}
2. 输入一个圆半径(r)当r>=0时,计算并输出圆的面积和周长,否则,输出提示信息。
#include <stdio.h>
#define PI 3.14 <stdio.h>
main()
{ float r ,s , l;
printf("please input r:\n");
scanf("%f”,&r);
if (r>=0)
{s=pi*r*r;
l=2*i*r ;
printf("the area is %f\n",s);
printf("the circumference is %f\n",l);}
else
printf("input error!\n");
}
3、函数y=f(x)可表示为: 2x-1 (x>0)
编程实现输入一个x值,输出y值。
main()
{int x,y;
scanf(“%d”,&x);
If(x<0)y=2*x+1;
If(x>0)y=2*x-1;
If(x==0) y=0;
printf(“%d”,y);}
4、编写一个程序,从4个整数中找出最小的数,并显示此数。
main( )
{int a,b,c,d,t;
scanf (“%d,%d,%d,%d ”,&a,&b,&c,&d);
if (a>b)
{t=a; a=b; b=t;}
if (a>c)
{t=a; a=c; c=t;}
if (a>d)
{t=a; a=d; d=t;}
printf (“min = %d \n”,a);
}
5.有一函数当x<0时y=1,当x>0时,
程,从键盘输入一个x值,输出y值。
main()
{int x,y;
scanf("%d",&x);
if (x<0) y=1;
else if(x==0) y=5;
else y=3;
printf("x=%d,y=%d\n",x,y);}
y=3,当x=0时y=5,编
6.从键盘输入两个数,求出其最大值(要求使用函数完成求最
大值,并在主函数中调用该函数)
main()
{float max(float x,float y);
float a,b,m;
scanf("%f,%f",&a,&b);
m=max(a,b);
printf("Max is %f\n",m);
}
float max(float x,float y)
{
float temp;
if (x<y)
{temp=x;
x=y;
y=temp;
}
return(x);
}
7、从键盘输入你和你朋友的年龄,编成判断谁的年龄最大,并打印最大者的年龄。
#include <stdio.h>
main()
{ int yourAge, hisAge;
printf("Please enter your age:");
scanf("%d", &yourAge); /*输入你的年龄yourAge*/ printf("Please enter your friend's age:");
scanf("%d", &hisAge); /*输入你朋友的年龄
hisAge*/
if (yourAge >= hisAge)
{
printf("You are older! Your age is = %d\n", yourAge); }
if (hisAge > yourAge)
{
printf("Your friend is older! HisAge age is = %d\n", hisAge);
}}
篇二:c案例
1、printf的使用案例
#include"stdio.h"
void main()
{
int a=65;
float d=5.8;
char c='a';
printf("output variale:%d,%6.3f,%c\n",a,d,c);
}
2、getchar的使用案例
#include"stdio.h"
void main()
{
char c;
printf("please input a char\n");
c=getchar();
printf("%c\n",c);
printf("%d\n",c);
}
3、/*将小写字母转换成大写字母*/
#include"stdio.h"
void main()
{
/*定义变量*/
char c;
/*设计输入*/
c=getchar();
/*将c小写字母转换成大写字母*/
c=c-32;
/*设计输出*/
printf("转换成的大写字母是:%c\n",c);
}
4、//将一个数逆序产生新的数
#include "stdio.h"
void main()
{
//定义变量
int oldnumber,newnumber;
int n1,n2,n3,n4;
//设计输入
scanf("%d",&oldnumber);
//分离出各个数位的值
n1=oldnumber%10; //求出个位数字
n2=oldnumber/10%10;//求出十位数字
n3=oldnumber/100%10; //求出百位数字
n4=oldnumber/1000; //求出千位数字
//产生新的四位数
newnumber=n1*1000+n2*100+n3*10+n4;
//设计输出
printf("新的四位数是:%d\n",newnumber);
}
5、关系运算符和逻辑运算符
? 判断是闰年,
条件二者居其一:1、能被4整除,但不能被100整除,2、能被400整除。
6、//输入三角形的三条边,求三角形的面积
#include"stdio.h"
#include"math.h"
void main()
{
//定义变量
float a,b,c,s,area;
//设计输入
printf("请输入三条边:\n");
scanf("%f%f%f",&a,&b,&c);
//计算面积
s=1.0/2*(a+b+c);
area=sqrt(s*(s-a)*(s-b)*(s-c));
//设计输出
printf("area=%f",area);
}
7、//输入圆半径,求圆的面积和周长。圆周率的值取为.14。
#include"stdio.h"
#include"math.h"
void main()
{
//定义变量 //设计输入 float r,c,s;
scanf("%f",&r);
}
8、if语句的使用。
输入两个整数,输出其中的较大数
void main( )
{int a,b,max;
printf(“\n input two numbers:”);
scanf("%d%d",&a,&b);
max=a;
if(max<b) max=b;
printf("max=%d",max);
} //计算圆的面积和周长 s=3.14*r*r; //设计输出 c=2*3.14*r; printf("面积=%f 周长=%f\n",s,c);
9、if语句的使用。
//判断是否为闰年
#include"stdio.h"
void main()
{
//定义变量
int year;
//设计输入
printf("请输入年份:");
scanf("%d",&year);
//判断是否为闰年
if(year%4==0&&year%100!=0||year%400==0)
printf("%d is a leap\n",year);
else
printf("%d is not a leap\n",year);
}
10、//输入两个数,如果两个数相等,就输出1,如果不相等,就输出0
#include"stdio.h"
void main()
{
//定义变量
int a,b;
//设计输入
scanf("%d%d",&a,&b);
//判断两数是否相等,输出相应结果
if(a==b)
printf("1\n");
else
printf("0\n");
}
11、//从键盘上接收一个字符,如果是字母,就输出“字母”,如果是数字,就输出“数字”。 #include"stdio.h"
void main()
{
char ch;
printf("请输入一个字符:");
scanf("%c",&ch);
if(ch>='0'&&ch<='9')
printf("数字\n");
if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z')
printf("字母\n");
}
12、if语句第三种形式的应用
//从键盘上输入一个字符,判断输出是数字字符、大写字符、小写字符还是其他字符。 #include"stdio.h"
void main()
{
//定义变量
char ch;
//设计输入
//ch=getchar();
scanf("%c",&ch);
//进行判断,输出
if(ch>='0'&&ch<='9')
printf("%c is digit letter!\n",ch);
else if(ch>='a'&&ch<='z')
printf("%c is low letter!\n",ch);
else if(ch>='A'&&ch<='Z')
printf("%c is upper letter!\n",ch);
else
printf("%c is other letter!\n",ch);
}
13、条件运算符的应用
//从键盘上接收一个字符,将其转换成大写字母输出,
//如果是小写字母就将其转换成大写字母输出,如果是大写字母就不变 #include"stdio.h"
void main()
{
//定义变量
char ch
;//设计输入
ch=getchar();
//将小写字母转换成大写字母
ch=ch>='a'&&ch<='z'?ch-32:ch;
//设计输出
printf("大写字母是:%c",ch);
}
14、//从键盘上输入一个数,如果不是0,则判断其正负和奇偶。 #include"stdio.h"
void main()
{
//定义变量
int num;
//设计输入
scanf("%d",&num);
//判断正负和奇偶,并输出
if(num==0)
printf("是零,不处理!\n");
if(num>0)
printf("%d 是正数!\n",num);
if(num<0)
printf("%d 是负数!\n",num);
if(num!=0&&num%2==0)
printf("%d 是偶数!\n",num);
if(num!=0&&num%2!=0)
printf("%d 是奇数!\n",num);
}
15、swtich语句的使用
//从键盘输入一个数字,则输出一个英文单词。
#include"stdio.h"
void main( )
{ int a;
篇三:C语言简单的23 个例子
例1.1:输入两个数,输出其中的最大者 方法一:
#include "stdio.h"
int main()
{
int a,b,max;
scanf("%d %d",&a,&b);
if (a>b) max=a;
else max=b;
printf("%d\n",max);
return 0;
}
方法二:
#include"stdio.h"
int main()
{
int m,n;
scanf("%d %d",&m,&n);
printf("%d\n",(m>n)?m:n);
return 0;
}
例1.2:输入三个数,输出其最大者
#include "stdio.h"
int main()
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
printf("%d\n",a>b?(a>c?a:c):(b>c?b:c));
return 0;
}
例2:求一个数的绝对值
方法一:
#include "stdio.h"
int main()
{
int a,absa;
printf("enter one number:\n");
scanf("%d",&a);
if (a<0)
absa=-a;
else
absa=a;
printf("|%d|=%d\n",a,absa);
return 0;
}
方法二:
#include"stdio.h"
#include"math.h"
int main()
{
int a;
scanf("%d",&a);
printf("%d\n",abs(a));
return 0;
}
例3:两个数的四则运算
#include "stdio.h"
int main()
{
double x,y;
char op;
printf("输入运算式:\n");
scanf("%lf%c%lf",&x,&op,&y);
switch (op)
{
case '+':printf("%.2f%c%.2f=%.2f\n",x,op,y,x+y);
break;
case '-':printf("%.2f%c%.2f=%.2f\n",x,op,y,x-y);
break;
case '*':printf("%.2f%c%.2f=%.2f\n",x,op,y,x*y);
break;
case '/':
if (y==0)
printf("error!\n");
else
printf("%.2f%c%.2f=%.2f\n",x,op,y,x/y);
break;
default :printf("expression is error!\n");
}
return 0;
}
例4:求N个数的平均数,以-1结束输入,且-1不纳入计算
#include "stdio.h"
int main()
{
int n=0;
double a,sum=0;
while(1)
{
scanf("%lf",&a);
if(a==-1) break;
sum+=a;
n++;
}
printf("%.2f\n",sum/n);
return 0;
}
例5:打印出九九乘法口诀
#include "stdio.h"
int main()
{
int i,j;
for (i=1;i<10;i++)
{ for(j=i;j<10;j++)
{
printf("%dx%d=%-4d",i,j,i*j);
}
printf("\n");
}
return 0;
}
例6:大小写转换,以数字0结束输入
#include "stdio.h"
int main()
{
char ch;
while(1)
{
scanf("%c",&ch);
if(ch=='0') break;
if(ch>='A'&&ch<='Z')
printf("%c\n",ch+32);
if(ch>='a'&&ch<='z')
printf("%c\n",ch-32);
} return 0;
例7:打印菱形图案
#include "stdio.h"
int main()
{
int i,j,k;
for (i=0;i<10;i++)
{
for (j=0;j<=9-i;j++)
printf(" ");
for (k=0;k<=2*i;k++)
printf("%c",3);
printf("\n");
}
for (i=0;i<=9;i++)
{
for (j=0;j<=i+1;j++)
printf(" ");
for (k=0;k<=16-2*i;k++)
printf("%c",3);
printf("\n");
}
return 0;
}
例8:求100以内所有的素数
#include "stdio.h"
#include "math.h"
main()
{
int m,i,k,t=0,j=1,n=0;
printf("%3d:",j);
for (m=2;m<=300;m=m+1)
{
k=(int)sqrt(m);
for (i=2;i<=k;i++)
if(m%i==0)
break;
if(i>=k+1)
{
printf("%5d",m);
} if(t%4==0) printf("\n%3d:",++j);} } printf("\n"); return 0;
例9:输出Fibonacci数列的前20项及其和 (Fibonacci数列:1,1,2,3,5,8,13,21···)
#include "stdio.h"
int main()
{
int i,sum=0,f[20]={1,1};
for (i=2;i<20;i=i+1)
f[i]=f[i-1]+f[i-2];
for (i=0;i<20;i=i+1)
{
printf("%6d",f[i]);
sum=sum+f[i];
if ((i+1)%4==0) printf("\n");
}
printf("sum=%d\n",sum);
return 0;
}
例10:输出各位数均不相同的三位数,及其个数
#include "stdio.h"
int main()
{
int n,i,j,k;
n=0;
for (i=1;i<=9;i++)
for (k=1;k<=9;k++)
if (k!=i)
for (j=0;j<=9;j++)
if(j!=i&&j!=k)
{
n++;
printf("%d ",100*i+10*j+k);
if (n%10==0) printf("\n");
}