C語(yǔ)言課程訓(xùn)練系統(tǒng)題-基礎(chǔ)習(xí)題
1.愛(ài)因斯坦
#include <stdio.h>
main()
{
int x,find=1;
x=0;
do{
x ;
if(x%2==1&&x%3==2&&x%5==4&&x%6==5&&x%7==0)find=0;
}while (find);
printf("x=%d\n",x);
}
2.輸出兩數(shù)最大值
#include<stdio.h>
main()
{
int a,b,max;
printf("Input a, b:");
scanf("%d,%d",&a,&b);
if (a>b) max = a;
if (a<=b) max = b;
printf("max = %d\n",max);
}
3.輸出兩數(shù)商
#include <stdio.h>
main()
{
int a,b;
double c;
printf("Input two integers:");
scanf("%d%d",&a,&b);
c = a/b;
printf("The quotient of a and b is :%.f",c);
}
4.12a4.2
#include <stdio.h>
main()
{
int i;
char ch;
float f;
printf("Please input:\n");
scanf("%d%c%f",&i,&ch,&f);
printf("The input integer is : %-3d\nThe input character is : %c\n",i,ch);
printf("The input float is : %f",f);
}
5判斷3個(gè)數(shù)是否相等
#include <stdio.h>
main()
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if (a==b&&a==c)
printf("The three number is equal!!!");
else
printf("The three number isn't equal!!!");
}
6輸入一個(gè)數(shù),逆序輸出這個(gè)數(shù)
#include<stdio.h>
main()
{
int x,a,b,c,d,y;
printf("Input x:");
scanf("%d",&x);
if(x<0)
d=(-x);
else
d=x;
a=d/100;
b=(d-a*100)/10;
c=d;
y=a b*10 c*100;
printf("y = %d\n",y);
}
7求三角形面積
#include<stdio.h>
#include<math.h>
main()
{
float a,b,c,s,area;
printf("Enter 3 floats");
scanf("%f,%f,%f",&a,&b,&c);
s=(a b c)/2;
area=(float)sqrt(s*(s-a)*(s-b)*(s-c));
printf("area=%.2f\n",area);
}
8四則運(yùn)算
#include<stdio.h>
#include<math.h>
main()
{
float a,b;
char op;
printf("Please enter the expression:\n");
scanf("%f %c%f",&a,&op,&b);
switch(op)
{
case' ':printf("%f %f = %f \n",a,b,a b);break;
case'-':printf("%f - %f = %f \n",a,b,a-b);break;
case'*':printf("%f * %f = %f \n",a,b,a*b);break;
case'^':printf("%f ^ %f = %f \n",a,b,pow(a,b));break;
case'/':if(b==0)
printf("Division by zero!\n");
else
printf("%f / %f = %f \n",a,b,a/b);break;
default:printf("Invalid operator! \n");
}
}
9求2/1,3/2,5/3,8/5,13/8,21/13,…前20項(xiàng)之和
#include <stdio.h>
main()
{
double i, s1 = 2, s2 = 1;
float x, sum = 0;
for (i = 1; i <= 20; i )
{
sum =( s1 / s2);
x = s1;
s1 = s2;
s2 = x;
}
printf("sum = %f\n", sum);
}
10小寫轉(zhuǎn)大寫
#include<stdio.h>
main()
{
char c1,c2;
c1=getchar();
c2=c1-32;
printf("%c,%d\n",c2,c2);
}
11大寫轉(zhuǎn)小寫
#include<stdio.h>
main()
{
char c1,c2;printf("Press a key and then press Enter:");
c1=getchar();
c2=c1 32;
printf("%c\n",c2);
}
12輸入兩數(shù)求商
#include <stdio.h>
main()
{
int a,b,c;
printf("Enter two numbers");
scanf("%d%d",&a,&b);
if(b==0)
printf("cannot divide by zero.\n");
else
c=a/b;
printf("%d",c);
}
13計(jì)算心跳次數(shù)
#include<stdio.h>
#include<math.h>
main()
{
int n,c;
printf("Please input your age: ");
scanf("%d",&n);
c=n*365*24*60*75;
printf("The heart beats in your life: %d",c);
}
14輸出指定文字
#include<stdio.h>
main()
{
printf("*****************************\n");
printf("* C programming *\n");
printf("* Hello world! *\n");
printf("*****************************\n");
}
15溫度轉(zhuǎn)換
#include<stdio.h>
#include<math.h>
main()
{
double t,T;
printf("Please input fahr: ");
scanf("%lf",&t);
T=5.0*(t-32.0)/9.0;
printf("The cels is: %.2f",T);
}
#include<stdio.h>
#include<math.h>
main()
{
double t,T;
printf("Please input cels: ");
scanf("%lf",&t);
T=t*9.0/5.0 32.0;
printf("The fahr is: %.2f",T);
}
16體重指數(shù)
#include<stdio.h>
#include<math.h>
main()
{
int w,h,weight;
double height,t;
printf("Input weight, height:\n");
scanf("%d,%d",&w,&h);
weight=w*2;
height=h/100.00;
t=w/(height*height);
printf("weight=%d\n",weight);
printf("height=%.2f\n",height);
printf("t=%.2f\n",t);
}
17大象喝水
#include<stdio.h>
#include<math.h>
main()
{
int h,r,n;
float PAI=3.14159;
scanf("%d,%d",&h,&r);
n=20000/(h*r*r*PAI);
printf("please input the height and the radius:\n%d",n 1);
}
18輸出大寫字母,所占內(nèi)存大小
#include<stdio.h>
main()
{
char c1,c2;
printf("please input a lowercase:\n");
c1=getchar();
c2=c1-32;
printf("%c %d %d\n",c2,c2,sizeof(c2));
}
19改錯(cuò)12a4.
#include <stdio.h>
main()
{
int i;
char ch;
float f;
printf("Please input:\n");
scanf("%d%c%f",&i,&ch,&f);
printf("The input integer is : %d \nThe input character is : %c\n", i, ch);
printf("The input float is : %f", f);
}
20輸出N個(gè)階乘
#include<stdio.h>
#include<math.h>
main()
{
int i,n;
long p=1;
printf("Please enter n:");
scanf("%d",&n);
for(i=1;i<=n;i )
{
p=p*i;
printf("%d! = %ld\n",i,p);
}
}
注:其余39道基礎(chǔ)題在我資源文檔中。
來(lái)源:https://www./content-4-799351.html