练习代码
1
#include<stdio.h>
int main(){
int num=1;
while(num<21){
num++;
printf("%d %5d/n",num,num*num);
}
printf("That's is OK!");
getch();
return 0;
}
2
#include<stdio.h>
int main(){
int a,b,c;
a=0;
b=0;
scanf("%d",&c);
while(a++<c){
b=b+a;
}
printf("%d",b);
getch();
return 0;
}
3
#include<stdio.h>
int main(){
char a;
int b;
float c;
c=b=a='A';
printf("char =%c int=%d,float=%f/n",a,b,c);
a=a+2;
b=c+3*a;
c=2.0*a+b;
printf("char =%c int=%d,float=%f/n",a,b,c);
a=22222.22;
printf("%c/n",a);
getch();
return 0;
}
运行结果
char =A int=65,float=65.000000
char =C int=266,float=400.000000
4
#include<stdio.h>//定义一个带参数的函数
void pound(int n);
void butler(void); //第一个void表示函数不返回值,第二个void表示函数不带函数
int main(){
int times=5;
int ch='a';
float f=19.003;
pound(times);
pound('q');
pound((int)f);//指派运算符
butler();
getch();
return 0;
}
void pound(int n){
while(n-->0)
printf("*");
printf("/n");
}
void butler(void){
printf("hahaha");
}
运行结果
*****
********************************************************************************
*********************************
*******************
hahaha
5
#include<stdio.h>
int main(){
int x,y,z;
x=(3/5)*220.0;//为0是因为3和5都是整数,相除后自动变成int,截尾后为0
y=22.0*3/5;
z=(2+2.9)*4;
printf("%d,%d,%d",x,y,z);
getch();
return 0;
}
运行结果:
0,13,19
6
#include<stdio.h>
int main(){
const int P=60;
int min;
printf("please enter:");
scanf("%d",&min);
while(min>0){
printf("%d %d/n",min/P,min%P);
printf("please enter:");
scanf("%d",&min);
}
printf("done!/n");
getch();
return 0;
}
7
#include<stdio.h>
int main(){
int num,num_1;
printf("please enter:");
scanf("%d",&num);
num_1=num;
while(num<num_1+10){
printf("%d/n",num);
num++;
}
printf("%d/n",num);
getch();
return 0;
}
8
#include <stdio.h> //返回值的问题,值得注意
float square(float n);
int main(){
float num;
printf("please enter:");
scanf("%f",&num);
num=square(num);
printf("%f",num);
getch();
return 0;
}
float square(float n){
n= n*n;
return n;
}
9
编写一个程序,该程序要求用户输入一个华氏温度。程序以Double类型读入温度值,并将它作为一个参数传递给用户提供的函数Temperature()。该函数将计算相应的摄氏温度和绝对温度,并以小数点右边有两位数字的精度显示这三种温度。它应该用每个值所代表的温度刻度来标识这3个值。下面是将华氏温度转换成摄氏温度的方程:
Celsius = 1.8 *Fahrenheit + 32.0.
下面是将摄氏温度转换成绝对温度的方程:
Kelvin = Celsius + 273.16
Temperature()函数使用const来创建代表该转换里的3个常量符号。main()函数将使用一个循环来允许用户重复地输入温度,当用户输入q或其他非数字值时,循环结束。
#include<stdio.h>
void Temperautre(double Fahrenheit);
int main(void)
{
double Fahrenheit;
printf("Please enter :/n");
while(scanf("%lf",&Fahrenheit)==1){
Temperautre(Fahrenheit);
printf("Please enter another value:");
}
return 0;
}
void Temperautre(double Fahrenheit)
{
const double a=1.8;
const double b=32.0;
const double c=273.16;
const double Celsius = a * Fahrenheit + b;
const double Kelvin = Celsius + c;
printf("%.2f fahrenheit is %.2f celsius or %.2f kelvin/n",Fahrenheit,Celsius,Kelvin);
}