1.猜数字
#include<stdlib.h>
#include<stdio.h>main(){
int guess;
int magic;
int counter=0;
magic=rand()%100+1;
while (guess!=magic){
printf("请输入数字:");
scanf("%d",&guess);
counter++;
if (guess>magic){
printf("错误,太大了\n");}
else if(guess<magic){
printf("错误,太小了\n");}
else {
printf("正确");
printf("这个数是= %d\n",magic);}
}while (guess!=magic)
printf("counter=%d\n",counter);}
2.字符大小的判断
#include<stdio.h>
main(){
char ch;
printf("请输入字母:");
scanf("%c",&ch);
if (('a'<=ch)&&(ch<='z'))
{
printf("小写字母=%c\n",ch);}
else if(('A'<=ch)&&(ch<='Z')){
printf("大写字母=%c\n",ch);}
else {printf("不是字母\n");}
}
3.男女身高的预测
#include<stdio.h>
main(){
float faheight,maheight;
float gheight,boyheight;
int t;
printf("请输入1或2");
scanf("%d",&t);
if (t==1)
{
printf("请输入父亲的身高和母亲的身高\n");
scanf("%f%f\n",&faheight,&maheight);
gheight=(faheight+maheight)*0.50;
printf("女孩身高%f",gheight);
}
4.阶乘
#include<stdio.h>
main(){
int n;
int i=1;
long p=1;
printf("请输入n的值:");
scanf("%d",&n);
while (i<=n){
p=p*i;
i++;
}
printf("%d! =ld\n",n,p);}