教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 高等教育 >

C语言程序设计实验与习题答案

来源:网络收集 时间:2026-08-03
导读: 重庆大学出版社 2.程序填空 (1)以下程序的功能是计算1~50之间能被7整除的整数之和。 #includestdio.h void main() {int i,sum= 0 ; for(i=1; 50 ;i++) if( i%7==0) sum+=i; printf(sum=%d\n,sum); } (2) 下面程序接收来自键盘的输入,直到输入Ctrl+Z(值为-1

重庆大学出版社

2.程序填空

(1)以下程序的功能是计算1~50之间能被7整除的整数之和。 #include<stdio.h>

void main()

{int i,sum= 0 ;

for(i=1; 50 ;i++)

if( i%7==0) sum+=i;

printf("sum=%d\n",sum);

}

(2) 下面程序接收来自键盘的输入,直到输入<Ctrl>+Z(值为-1)键为止。这些字符被原样输出,但若有连续一个以上的空格时只输出一个空格。请填空。

#include <stdio.h>

void main()

{char cx;

char front=’ ’;

while ( (cx=getchar())!=’\n’)

{if (cx!=’ ’)

putchar(cx);

if (cx= =’ ’)

if (front!=’ ’)

putchar(cx);

front=cx;

}

}

3.程序改错

(1)下列程序的功能是求1+3+5+ 99的和。

#include <stdio.h>

重庆大学出版社

void main( )

{ int s,i; ★ //int s=0,i;

i=1;

while(i<=99) s=s+i; ★ //{s=s+i;i++;}

printf("1+3+5+ 99的和是:%d\n",s);

}

(2)下面程序的功能是输入一个正整数,判断是否是素数,若为素数输出1,否则输出0。

#include <stdio.h>

void main()

{ int i,x,y=0; ★ //y=1

scanf("%d",&x);

for(i=2;i<=x/2&&y;i++)

if ((x%i)!=0) y=0; ★ //x%i==0

printf("%d\n",y);

}

4.设计性实验

(1)题

/* 方法(1)精度控制 */

#include <stdio.h>

#include <math.h>

main()

{ int s;

重庆大学出版社

float n,t,pi;

t=1; pi=0; n=1.0; s=1;

while((fabs(t))>=1e-6)

{ pi=pi+t;

n=n+2;

s=-s;

t=s/n;

}

pi=pi*4;

printf("pi=%10.6f\n",pi);

}

/* 方法(2)次数控制*/

#include <stdio.h>

#include <math.h>

main()

{ int s;

long times;

float n,t,pi;

t=1; pi=0; n=1.0; s=1;

for(times=1;times<=1e9;times++)

{ pi=pi+t;

n=n+2;

s=-s;

t=s/n;

}

pi=pi*4;

printf("pi=%10.6f\n",pi);

}

(2)题

main()

{

int i,j,frame;

double wheattal=0;

double wheatfnu=1;

printf("Please input frame’s numbers:");

scanf("%d",&frame);

for(i=0;i<frame;i++)

{

wheattal+=wheatfnu;

wheatfnu+=wheatfnu;

重庆大学出版社

}

printf("\n Total wheattatol’s timeter=%e\n",wheattal/1.40e8);

}

(3)题

/*方法一:使用递推公式n=n+2*/

main()

{

int i,n=1;

double s=0,t=1;

for(i=1;i<=20;i++)

{

t*=n;

s+=t;

n+=2;

}

printf("s=%lf",s);

getch();

}

/*方法二:使用通项公式2*i+1*/

main()

{

int i;

double s=0,t=1;

for(i=1;i<=20;i++)

{

t*=2*i+1;

s+=t;

}

printf("s=%lf",s);

getch();

}

重庆大学出版社

/*方法三*/

#include "stdio.h"

main()

{

long total,sum,m,n,t;

total=0;

for(m=1;m<=20;m++)

{

sum=1;t=1;

for(n=1;n<=m;n++) { sum=sum*t; t=t+2;}

total=total+sum;

}

printf("total=%ld",total);

}

2.程序填空

(1)以下程序的功能是计算1~50之间能被7整除的整数之和。 #include<stdio.h>

void main()

{int i,sum= 0;

for(i=1; 50 ;i++)

if( i%7==0 ) sum+=i;

printf("sum=%d\n",sum);

}

(2) 下面程序接收来自键盘的输入,直到输入<Ctrl>+Z(值为-1)键为止。这些字符被原样输出,但若有连续一个以上的空格时只输出一个空格。请填空。

重庆大学出版社

#include <stdio.h>

void main()

{char cx;

char front=’ ’;

while ( (cx=getchar())!=’\n’)

{if (cx!=’ ’)

putchar(cx);

if (cx= =’ ’)

if (front!=’ ’)

putchar(cx);

front=cx;

}

}

3.程序改错

(1)下列程序的功能是求1+3+5+ 99的和。

#include <stdio.h>

void main( )

{ int s,i; ★ //int s=0,i;

i=1;

while(i<=99) s=s+i; ★ //{s=s+i;i++;}

printf("1+3+5+ 99的和是:%d\n",s);

}

(2)下面程序的功能是输入一个正整数,判断是否是素数,若为素数输出1,否则输出0。

#include <stdio.h>

void main()

{ int i,x,y=0; ★ //y=1

重庆大学出版社

scanf("%d",&x);

for(i=2;i<=x/2&&y;i++)

if ((x%i)!=0) y=0; ★ //x%i==0

printf("%d\n",y);

}

4.设计性实验

(1)题

/* 方法(1)精度控制 */

#include <stdio.h>

#include <math.h>

main()

{ int s;

float n,t,pi;

t=1; pi=0; n=1.0; s=1;

while((fabs(t))>=1e-6)

{ pi=pi+t;

n=n+2;

s=-s;

t=s/n;

}

pi=pi*4;

printf("pi=%10.6f\n",pi);

}

/* 方法(2)次数控制*/

#include <stdio.h>

#include <math.h>

main()

{ int s;

long times;

float n,t,pi;

t=1; pi=0; n=1.0; s=1;

for(times=1;times<=1e9;times++)

{ pi=pi+t;

n=n+2;

重庆大学出版社

t=s/n;

}

pi=pi*4;

printf("pi=%10.6f\n",pi);

}

(2)题

main()

{

int i,j,frame;

double wheattal=0;

double wheatfnu=1;

printf("Please input frame’s numbers:");

scanf("%d",&frame);

for(i=0;i<frame;i++)

{

wheattal+=wheatfnu;

wheatfnu+=wheatfnu;

}

printf("\n Total wheattatol’s timeter=%e\n",wheattal/1.40e8);

}

(3)题

/*方法一:使用递推公式n=n+2*/

main()

{

int i,n=1;

double s=0,t=1;

for(i=1;i< …… 此处隐藏:2685字,全部文档内容请下载后查看。喜欢就下载吧 ……

C语言程序设计实验与习题答案.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/128452.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)