第3章三、编程题1. 编写程序,输入一个非负数,输出以此数为半径的圆周长以及面积。
#include "stdio.h"#define PI 3.1415void main(){float r,area,circumference;scanf("%f",&r);area=PI*r*r;circumference=2*r*PI;printf("area=%6.2f\ncircumference=%6.2f\n",area,circumference);}2. 编写程序,输出下面结果,注意,双引号也要输出:“I'm a student!”#include <stdio.h>void main(){printf("\"I\'m a student!\"\n"); }3. 编写程序,输入一个小写字母,将其转换为大写字母输出。
例如输入b ,则输出B 。
提示:小写字母和对应的大写字母的ASCII 码值相差32。
void main(){char ch;ch=getchar();ch-=32;putchar(ch);//printf("%c",ch);}4. 编写程序,输入一个华氏温度f ,输出其相应的摄氏温度c 。
华氏温度和摄氏温度的转换公式为:)32f (95c -=#include <stdio.h>void main(){float f,c;scanf(“%f ”,&f);c=5.0*(f-32)/9;printf(“华氏温度%5.2f 转换为摄氏温度为:%5.2f\n ”,f,c);第4章三、编程题1.输入一个整数,判断这个整数是奇数还是偶数(提示:整数的奇偶性可以利用取余运算符%判定)。
#include <stdio.h>void main(){int a;scanf("%d",&a);if(a%2)printf("奇数\n");elseprintf("偶数\n");}2.编写程序,输入一个24小时制的时间,转换为12小时制时间后进行输出。
以13点15分为例,输入:13:15,则输出:下午1:15。
#include <stdio.h>void main(){int hour,minute;scanf("%d:%d",&hour,&minute);if (hour>12) hour=hour-12;printf("%d:%d\n",hour,minute);}3.输入年号,判断它是否是闰年(如果年号能被400整除,或能被4整除,而不能被100整除,则是闰年,否则不是)。
void main(){int year;scanf("%d",&year);if (year%400==0||(year%4==0&&year%100==0))printf("%d是闰年\n",year);elseprintf("%d不是闰年\n",year);}4.输入一个字符,如果是大写字母则输出对应的小写字母,如果是小写字母则输出相应的大写字母,如果都不是则原样输出。
#include <stdio.h>void main(){char ch;scanf("%c",&ch);if(ch>='a'&&ch<='z')ch-=32;elseif(ch>='A'&&ch<='Z')ch+=32;printf("\n%c\n",ch);}5.设计一个简单的计算器程序,能输入整型运算数和基本运算符(+,-,*,/),输出计算结果。
例如:输入2+6,输出2+6=8。
#include<stdio.h>main(){float a,b,result;char op;scanf("%f%c%f",&a,&op,&b);switch(op){case'+': result=a+b; printf("=%f",result); break;case'-': result=a-b; printf("=%f",result); break;case'*': result=a*b; printf("%f",result); break;case'/': if(b==0.0) printf("error!\n");result=a/b;printf("=%f",result);default: printf("error due to the illegal input!\n");}}第5章三、编程题1.编写程序,显示100~200之间能被7除余2的所有整数。
#include "stdio.h"main(){ int i;for(i=100;i<=200;i++){ if(i%7==2)printf("\t%d\t",i);}}2.输入n个整数,求这n个整数中的最大数、最小数和偶数平均数,并输出。
#include <stdio.h>void main(){int i,n,data,max=0,min=0,even=0,evennumber=0;printf("Please input the number of data:");scanf("%d",&n);printf("Please input the data:\n");scanf("%d",&data);max=data;min=data;if (data%2==0){even=even+data;evennumber++;}for(i=1;i<n;i++){scanf("%d",&data);if (data>max)max=data;elseif (data<min)min=data;if (data%2==0){even=even+data;evennumber++;}}printf("The max is %d\nThe min is %d\n The average of even is %5.2f\n",max,min,(float)(even)/evennumber);}3.输入一串字符,以回车作为结束标志。
统计并输出这串字符中大写字母、小写字母和数字字符的个数。
#include <stdio.h>void main(){int upper=0,lower=0,number=0;char letter;while((letter=getchar())!='\n'){if (letter>='a'&&letter<='z') lower++;else if (letter>='A'&&letter<='Z') upper++;else if (letter>='0'&&letter<='9') number++;}printf("the number of uppercase is:%d\n",upper);printf("the number of lowercase is:%d\n",lower);printf("the number of number is:%d\n",number);}4.输出九九乘法表。
#include <stdio.h>void main(){int i,j;for(i=1;i<=9;i++){for(j=1;j<=i;j++)printf("%d*%d=%-3d ",i,j,i*j);printf("\n");}}5.编写程序,输出3~1000之间全部素数。
#include <stdio.h>#include "math.h"void main(){int k,data,tag;for(data=3;data<=1000;data++) //外层循环,用来产生2~1000之间的整数{tag=0; //tag用于表示数i是否是素数,没有判断前先假定是素数for(k=2;k<=sqrt(data)&&!tag;k++) //内层循环用来判断data是否有约数{if (data%k==0)tag=1;}if (tag==1) printf("%4d",data); //如果i是素数,则输出}}6.输入一个三位数,判断其是否是“水仙花数”。
水仙花数是指3位数中的各位数字的立方和等于这3位数本身。
如153=1*1*1+5*5*5+3*3*3。
#include"stdio.h"void main(){int S, a, b, c;printf("请输入一个3位数:");scanf("%d",&S);a=S/100; b=S%100/10; c=S%10;if(a*a*a+b*b*b+c*c*c==S)printf("%d是水仙花数。
",S);elseprintf("%d不是水仙花数。
\n",S);}7.编程求Fibonacci数列的前40个数。
该数列的生成方法是:F1=1,F2=1,F n=F n-1+F n-2(n>=3)(即从第三个数起,每个数等于前2个数之和)。