当前位置:
文档之家› C语言实验报告参考答案(原)汇总
C语言实验报告参考答案(原)汇总
y = pow(a,5) + sin(a*x) + exp(a*x) + log(a+x);
printf("y=%f\n",y);
return 0;
}
五、调试和测试结果
2(1)输入:12 14
输出:26
1a
2(2)输入:1 0
输出:2.000000
实验三选择结构程序设计
四、设计流程(算法描述)
(请写出上机内容2(3)题的算法描述)
{
if(i%j == 0)
sum +=j;
}
if(i == sum) //sum为因子和,如果和i相等,则输出
printf("%d ",i);
}
return 0;
}
3.编写程序:计算sinx的近似值,精确到10-6。
其实 所以程序
#include <stdio.h>
#include <math.h>
else
printf("the number is negetive\n");
return 0;
}
(2)输入两个整数a和b,若a>=b时,求其积c并显示;若a<b时,求其商c并显示。
#include<stdio.h>
main()
{
int a,b,c;
scanf("%d%d",&a,&b);
if(a>=b)
scanf("%d%d",&m,&n);
printf("s=%ld\n",power ( m,n));
return 0;
}
2.编写自定义函数prime(int x),判断x是否为素数。利用此函数编写程序找出3~5000中的所有素数,并输出素数的个数。
#include<stdio.h>
#include<math.h>
(a+b)/(b-c)*(c-d)=-1064.0000
3.编译、连接无错,运行后屏幕上显示以下结果:
c =-10
实验二顺序结构程序设计
四、程序清单
1
问题1D。
问题2改printf("%c,%c,%d\n",a,b,c);这条语句
改成:printf("%c %c %d\n",a,b,c);
问题3改scanf("%c%c%d",&a,&b,&c);这条语句
printf("The shoes are big\n");
printf("The trousers are black\n");
}
2.编写程序:
(1) a=150,b=20,c=45,编写求a/b、a/c(商)和a%b、a%c(余数)的程序。
(2)a=160,b=46,c=18,d=170,编写求(a+b)/(b-c)*(c-d)的程序。
for (i=1;i<=100;i++)
sum += i;
printf("the sum is:%d\n",sum);
sum =0;
for(i=1;i<=100;i++)
{
j=pow(i,2);
sum +=j;
}
printf("the square sum is:%d\n",sum);
return 0;
主要是两两比较,然后得出最大的数
五、程序清单
(1)输入一个整数,若大于等于0,输出提示信息“is positive”,否则输出“is negative”。
#include<stdio.h>
#include<math.h>
main()
{
int a;
scanf("%d",&a);
if(a>=0)
printf("the number is positve\n");
}
六、调试和测试结果
1:结果:the sum is:5050
the square sum is:338350
2:结果:6 28 496
3、输入0,输出sin(0.00)=0.000000
输入1.57,输出sin(1.57)=1.000000
输入0.5,输出sin(0.50)=0.479426
实验五函数和编译预处理
#include<stdio.h>
#include<math.h>
void Mad(int a[],int n)
{
int i;
a[0]=2;
a[1]=3;
for(i=2;i<n;i++)
int prime(int m)
{
int i,k;
k=sqrt(m);
for(i=2;i<=k;i++)
if(m%i==0)break;
if(i>k)return 1;
return 0;
}
main()
{
int i,k;
k=0;
for(i=3;i<=5000;i++)
if(prime(i)==1){k++;printf("%d is a prime muber \n",i);}
printf("c=%d\n",a*b);
else
printf("c=%d\n",a/b);
return 0;
}
(3)输入a、b、c三个整数,输出最大数。
#include<stdio.h>
main()
{
int a,b,c,x;
scanf("%d%d%d",&a,&b,&c);
if(a>=b)
x=a;
printf("a/c的余数=%d\n",y);
}
(2)
#include<stdio.h>
main()
{
int a,b,c,d;
float x;
a=160;
b=46;
c=18;
d=170;
x=(a+b)/(b-c)*(c-d);
printf("(a+b)/(b-c)*(c-d)=%f\n",x);
}
答案:
(1)
#include<stdio.h>
main()
{
int a,b,c,x,y;
a=150;
b=20;
c=45;
x=a/b;
y=a/c;
printf("a/b的商=%d\n",x);
printf("a/c的商=%d\n",y);
x=a%b;
y=a%c;
printf("a/b的余数=%d\n",x);
printf("共有%d个素数\n",k);
}
3.编写自定义函数count(int x),计算x的因子个数。利用此函数找出并输出1~1000中有奇数个不同因子的整数。
#include<stdio.h>
#include<math.h>
int count(int x)
{
int sum,i;
sum =0;//记住因子的个数
3.设变量a的值为0,b的值为-10,编写程序:当a>b时,将b赋给c;当a<=b时,将0赋给c。(提示:用条件运算符)
答案:
#include<stdio.h>
main()
{
int a,b,c;
a=0;
b=-10;
c= (a>b) ? b:a;
printf("c = %d\n",c);
}
五、调试和测试结果
1.输入:2 3
输出:s=8
2.输出:共有668个素数
2.
3、输出结果为:
实验六数组
四、设计流程(算法描述)
(请写出上机内容1的算法描述)
设置两个变量分别指示头和尾。第一个和最后一个元素值互换,然后头和尾变量向里移动,最终到两变量相遇为止。
五、程序清单
1.编写程序:从键盘输入一串整数保存到数组中,调用函数antitone()将数组反序输出。自定义函数void antitone(int a[],int n)实现将数组中的n个数据按逆序存放。
2(2)输入:3 2输出:c=6
输入:2 3输出:c=0
2(3)输入:3 2 1输出:the max number is:3
输入:2 3 1输出:the max number is:3
输入:1 2 3输出:the max number is:3
实验四循环结构程序设计
四、设计流程(算法描述)
(请写出上机内容2的算法描述)
改成scanf("%c%*c%c%*c%d",&a,&b,&c);
printf("\'%c\',\'%c\',%d\n",a,b,c);
2(1)从键盘输入两个八进制数,计算两数之和并分别用十进制和十六进制数形式输出。
#include <stdio.h>
int main()
{
int a,b,c;