[c语言指针编程练习题]C语言练习题精选
篇一 : C语言练习题精选
1. 从键盘上输入两个整数,求出它们的和
#include “stdio.h”
void main
{
int x,y,s;
scanf;
scanf;
s=x+y;
printf;
}
2. 已知三角形的底和高,求出三角形的面积。
#include “stdio.h”
void main
{
int x,y;
float s;
x=4;
y=5;
s=x*y/2.0;
printf;
}
3. 从键盘上输入三角形的底和高,求出三角形的面积。
#include “stdio.h”
void main
{
int x,y;
float s;
scanf;
scanf;
s=x*y/2.0;
printf;
}
4. 已知三角形的三边长,求出三角形的面积。
#include “stdio.h”
#include “math.h”
void main
{
int a,b,c;
float p,s;
a=3;
b=4;
c=5;
p=/2.0;
s=sqrt * *);
printf;
}
5. 已知二元一次方程的三个系数,求方程的一个根。
#include “stdio.h”
#include “math.h”
void main
{
int a,b,c;
float root;
a=3;
b=4;
c=5;
root=-4*a*c))/;
printf;
}
6. 编程实现符号函数。当x 0 ,则sgn=+1, 当x =0 ,则sgn=0
#include “stdio.h”
void main
{
float x;
int y;
scanf;
if ;
y=1;
if ;
y=0;
if ;
y=-1;
printf=%d”,x,y);
}
或者:
#include “stdio.h”
void main
{
float x;
int y;
scanf;
if ;
y=1;
else if ;
y=0;
else;
y=-1;
printf=%d”,x,y);
}
或者:
#include “stdio.h”
void main
{
float x;
int y;
scanf;
y= ? 1: ? 0 : -1;
printf=%d”,x,y);
}
7. 从键盘上输入数字星期,在屏幕上显示英文星期。
#include “stdio.h”
void main
{
float x;
scanf;
switch
{
case 0 :printf;break;
case 1 :printf;break;
case 2 :printf;break;
case 3 :printf;break;
case 4 :printf;break;
case 5 :printf;break;
case 6 :printf;break;
default :printf;
,
}
8. 从键盘输入三角形的三边长,判断出这三边能否构成三角形
#include “stdio.h”
void main
{
int a,b,c;
scanf;
scanf;
scanf;
if && >b) && >a )
printf;
else
printf;
}
9. 从键盘输入三个整数,将这三个数按从大到小的顺序排列起来
#include “stdio.h”
void main
{
int a,b,c,temp;
scanf;
scanf;
scanf;
if && && )
printf;
if && && )
printf;
if && && )
printf;
if && && )
printf;
if && && )
printf;
if && && )
printf;
}
或者这样写:
#include “stdio.h”
void main
{
int a,b,c,temp;
scanf;
scanf;
scanf;
if
{
temp=a;
a=b;
b=temp;
}
if
{
temp=a;
a=c;
c=temp;
}
if
{
temp=b;
b=c;
c=temp;
}
printf;
}
10. 从键盘输入二个整数,求出最大值。
#include “stdio.h”
void main
{
int a,b,temp;
scanf;
scanf;
if
temp=a;
else
temp=b;
printf;
}
11. 从键盘输入三个实数,求出最大值。
#include “stdio.h”
void main
{
float a,b,c,temp;
scanf;
scanf;
scanf;
if
temp=a;
else
temp=b;
if
temp=c;
printf;
}
12. 从键盘输入三个整数,在一个程序中既
出最大值,还要求
出最小值。
#include “stdio.h”
void main
{
int a,b,c,max,min;
scanf;
scanf;
scanf;
if
{max=a; min=b;}
else
{max=b;min=a;}
if
max=c;
if
min=c;
printf;
}
13. 使用for循环编程求出1到100的和
#include “stdio.h”
void main
{
int i,s=0;
for
s=s+i;
printf;
}
14. 使用当循环编程求出1到100的和
#include “stdio.h”
void main
{
int i=1,s=0;
while
{
s=s+i;
i++;
}
printf;
}
15. 使用当循环编程求出1到100的和
#include “stdio.h”
void main
{
int i=1,s=0;
do
{
s=s+i;
i++;
}
while ;
printf;
}
16. 从键盘输入10个实数,求出最大值
#include “stdio.h”
void main
{
int i;
float x,max;
scanf;
max=x;
for
{
scanf;
if
max=x;
}
printf;
}
15. 从键盘输入10个整数,求出最小值
#include “stdio.h”
void main
{
int i x,min;
scanf;
min=x;
for
{
scanf;
if
min=x;
}
printf;
}
16. 从键盘输入10个整数,求出它们的平均值
#include “stdio.h”
void main
{
int i a[10],s=0;
float aver=0.0;
for
{
scanf;
s=s+i;
}
aver=s/10.0;
printf;
}
17. 编程在屏幕上输出具有7行的正三角形
,
,,,
,,,,,
,,,,,,,
,,,,,,,,,
,,,,,,,,,,,
,,,,,,,,,,,,,
#include “stdio.h”
void main
{
int i , j;
for
{
for
printf;
for
printf;
printf;
}
,
18. 求出8的阶乘。
#include “stdio.h”
void main
{
int i ;
long p=1;
for
p=p*i;
printf;
,
从键盘输入20个整数,求出不大于90所有数的和
#include “stdio.h”
void main
{
int i ,s=0,x;
for
{
scanf;
if
s=s+x;
}
printf;
,
从键盘输入一个整数,判断这个数是否为素数
#include “stdio.h”
void main
{
int i ,x,flag=1;
scanf;
for
{
if
{
flag=0;
break;
}
}
if
printf;
else
printf;
,
从键盘输入20个整数,求出它们的和
#include “stdio.h”
void main
{
int i ,s=0,x[20];
for
{
scanf;
s=s+x[i];
}
printf;
,
从键盘输入20个整数,求出它们的平均值及比平均值大的数
#include “stdio.h”
void main
{
int i ,s=0,x[20];
float aver=0.0;
for
{
scanf;
s=s+x[i];
}
aver=s/20.0;
printf;
for
{
if
printf;
}
,
已知五个整数3,,5,8,2,9,求出最大值
#include “stdio.h”
void main
{
int i ,x[5]={ 3,,5,8,2,9},max;
max=x[0];
for
if
max=x[i];
printf;
,
从键盘输入20个整数,求出它们的最小值,并求最小值所在位置
#include “stdio.h”
void main
{
int i ,min,x[20],pos=0;
for
scanf;
min=x[0];
pos=0;
for
if
{
min=x[i];
pos=i;
}
printf;
,
已知五个整数3,,5,8,2,9,按从大到小的顺序排列起来
#include “stdio.h”
void main
{
int j ,k, t , b[5]={ 3,,5,8,2,9};
for
for
if
{
t= b[j];
b[j]= b[k];
b[k]=t;
}
for
printf;
}
从键盘输入10个实数,按从大到小的顺序排列起来
#include “stdio.h”
void main
{
int i, j ,k;
float b[10],t;
for
scanf;
for
for
if
{
t= b[j];
b[j]= b[k];
b[k]=t;
}
for
printf;
}
某班有43名学生,某门考试结束后,请按10段统计出各分数段的
人数
#include “stdio.h”
void main
{
int j , x[43],fragment[12];
for
scanf;
for
fragment[x[j]/10]= fragment[x[j]/10]+1;
for
printf;
}
或者写成:
#include “stdio.h”
void main
{
int j , x[43],t ,fragment[12];
for
scanf;
for
,
t= x[j]/10;
fragment[t]= fragment[t]+1;
,
for
printf;
}
从键盘上输入两个字符串,并将它们交换后输出
#include “stdio.h”
#include “string.h”
void main
{
int j ,k;
char ch1[81],ch2[81],t[81];
scanf;
scanf;
strcpy;
strcpy;
strcpy;
printf;
}
已知五个字符串”China”,”American”,”Japan”,”France”,”Australia”,编
程将它们按从小到大的顺序排列起来
#include “stdio.h”
#include “string.h”
void main
{
int j ,k;
char t[81], b[5][81]= ,”China”,”American”,”Japan”,”France”,”Australia”};
for
for
if )
{
strcpy;
strcpy;
strcpy;
}
for
printf;
}篇二 : 浙江大学数值分析C语言编程习题
C语言编程习题
第二章
习题2-2
5. 用二分法编程求 6x4 -40x2+9=0 的所有实根。[)
#include
#include
#define N 10000
double A,B,C;
double f
{
return ;
}
void BM
{
int k;
double x,xe;
double valuea = f;
double valueb = f;
if return;
printf; for {
x=/2;
xe=/2;
if printf;
printf=%g\n\n”,f);
return;
}
if*f else a=x;
}
printf;
}
1
int main
{
double a,b,eps1,eps2,step,start;
printf;
scanf;
printf;
scanf;
for double right = start + step;
BM;
}
return 0;
}
运行:
Please input A,B,C:
6 -40 9
Please input a,b, step, eps1,eps2:
-10 10 1 1e-5 1e-5
Finding root in the range: [-3.000, -2.000]
The x value is:-2.53643
f=-0.00124902
Finding root in the range: [-1.000, 0.000]
The x value is:-0.482857
f=0.00012967
Finding root in the range: [0.000, 1.000]
The x value is:0.482857
f=0.00012967
Finding root in the range: [2.000, 3.000]
The x value is:2.53643
f=-0.00124902
有时若把判别语句
2
if 改为
if 会提高精度,对同一题运行结果:
Finding root in the range: [-3.000, -2.000]
The x value is:-2.53644
f=-4.26496e-007
Finding root in the range: [-1.000, 0.000]
The x value is:-0.482861
f=-7.3797e-006
Finding root in the range: [0.000, 1.000]
The x value is:0.482861
f=-7.3797e-006
Finding root in the range: [2.000, 3.000]
The x value is:2.53644
f=-4.26496e-007
习题2-3
5. 请用埃特金方法编程求出x=tgx在4.5附近的根。[]#include
#include
#define N 100
#define PI 3.1415926
void SM
{
int k;
double x;
double x1,x2;
for {
x1=sin/cos;
x2=sin/cos;
x=x0-*/;
if 3
printf; return;
}
x0=x;
}
printf;
}
int main
{
double eps,x0;
printf;
scanf;
SM;
return 0;
}
运行:
Please input eps,x0:
1e-5 4.5
The x value is:4.49341
习题2-4
11(请编出用牛顿法求复根的程序,并求出 P=z4-3z3+20z2+44z+54=0
接近于z0=2.5+4.5i 的零点。
#include
#include
#define MAX_TIMES 1000
typedef struct {
double real, image;
} COMPLEX;
COMPLEX Aa[5]={{54,0},{44,0},{20,0},{-3,0},{1,0}};
COMPLEX Bb[4]={{44,0},{40,0},{-9,0},{4,0}};
COMPLEX zero = {0, 0};
4
double eps1=1e-6;
double eps2=1e-6;
COMPLEX multi {
COMPLEX result;
= a.real * b.real - a.image * b.image; result.image = a.image *
b.real + a.real * b.image; return result;
}
COMPLEX Div{ COMPLEX z3;
double s;
s=+;
=b.real;
z3.image=-b.image;
z3=multi;
= ;
z3.image=z3.image/s;
return z3;
}
COMPLEX add {
COMPLEX result;
= a.real + b.real;
result.image = a.image + b.image;
return result;
}
COMPLEX subtract {
COMPLEX result;
= a.real - b.real;
result.image = a.image - b.image;
return result;
}
COMPLEX times{
int i;
COMPLEX result={1, 0};
for result=multi; return result;
}
5
double distance {
return sqrt * + * ); }
double complex_abs {
return sqrt;
}
COMPLEX f
{
int i;
COMPLEX result=zero;
for {
result=add));
}
return result;
}
COMPLEX ff {
int i;
COMPLEX result=zero;
for {
result=add));
}
return result;
}
int main{
COMPLEX z0,z1,result;
double x,y;
int k;
printf;
scanf;
=x; z0.image=y;
for {
z1 = subtract, ff));
result = f;
if printf, f=\n”, ,z1.image, , result.image);
return 0;
6