全国旗舰校区

不同学习城市 同样授课品质

北京

深圳

上海

广州

郑州

大连

武汉

成都

西安

杭州

青岛

重庆

长沙

哈尔滨

南京

太原

沈阳

合肥

贵阳

济南

下一个校区
就在你家门口
+
当前位置:首页  >  技术干货

c语言经典练习题100道(四)

发布时间:2022-07-29 17:07:07
发布人:syq

  【程序16】

  题目:输入两个正整数m和n,求其最大公约数和最小公倍数。

c语言经典练习题

  1.程序分析:利用辗除法。

  2.程序源代码:

#include "stdio.h"

#include "conio.h"

main(){

 int a,b,num1,num2,temp;

 printf("please input two numbers:\n");

 scanf("%d,%d",&num1,&num2);

 if(num1<num2)/*交换两个数,使大数放在num1上*/

 {

 temp=num1;

 num1=num2;

 num2=temp;

 }

 a=num1;b=num2;

 while(b!=0)/*利用辗除法,直到b为0为止*/

 {

 temp=a%b;

 a=b;

 b=temp;

 }

 printf("gongyueshu:%d\n",a);

 printf("gongbeishu:%d\n",num1*num2/a);

 getch();

}

  【程序17】

  题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

  1.程序分析:利用while语句,条件为输入的字符不为'\n'.

  2.程序源代码:

#include "stdio.h"

#include "conio.h"

main(){

 char c;

 int letters=0,space=0,digit=0,others=0;

 printf("please input some characters\n");

 while((c=getchar())!='\n')

 {

 if(c>='a'&&c<='z'||c>='A'&&c<='Z')

 letters++;

 else if(c==' ')

 space++;

 else if(c>='0'&&c<='9')

 digit++;

 else

 others++;

 }

 printf("all in all:char=%d space=%d digit=%d others=%d\n",letters,

 space,digit,others);

 getch();

}

  【程序18】

  题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。

  1.程序分析:关键是计算出每一项的值。

  2.程序源代码:

#include "stdio.h"

#include "conio.h"

main(){

 int a,n,count=1;

 long int sn=0,tn=0;

 printf("please input a and n\n");

 scanf("%d,%d",&a,&n);

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

 while(count<=n)

 {

 tn=tn+a;

 sn=sn+tn;

 a=a*10;

 ++count;

 }

 printf("a+aa+...=%ld\n",sn);

 getch();

}

  【程序19】

  题目:一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3.编程找出1000以内的所有完数。

  1. 程序分析:请参照程序<--上页程序14.

  2.程序源代码:

#include "stdio.h"

#include "conio.h"

main(){

 static int k[10];

 int i,j,n,s;

 for(j=2;j<1000;j++)

 {

 n=-1;

 s=j;

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

 {

 if((j%i)==0)

 {

 n++;

 s=s-i;

 k[n]=i;

 }

 }

 if(s==0)

 {

 printf("%d is a wanshu",j);

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

 printf("%d,",k[i]);

 printf("%d\n",k[n]);

 }

 }

 getch();

}

  【程序20】

  题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?

  1.程序分析:见下面注释

  2.程序源代码:

#include "stdio.h"

#include "stdio.h"

main(){

 float sn=100.0,hn=sn/2;

 int n;

 for(n=2;n<=10;n++)

 {

 sn=sn+2*hn;/*第n次落地时共经过的米数*/

 hn=hn/2; /*第n次反跳高度*/

 }

 printf("the total of road is %f\n",sn);

 printf("the tenth is %f meter\n",hn);

 getch();

}

  更多关于“物联网培训”的问题,欢迎咨询千锋教育在线名师。千锋教育多年办学,课程大纲紧跟企业需求,更科学更严谨,每年培养泛IT人才近2万人。不论你是零基础还是想提升,都可以找到适合的班型,千锋教育随时欢迎你来试听。

相关文章

OKR与自驱力的关系是什么?

OKR与自驱力的关系是什么?

2023-10-14
office是什么软件类型?

office是什么软件类型?

2023-10-14
Cloud-IDE 是什么?

Cloud-IDE 是什么?

2023-10-14
主机和端系统之间有什么不同?

主机和端系统之间有什么不同?

2023-10-14

最新文章

常见网络安全面试题:Windows常用的命令有哪些?

常见网络安全面试题:Windows常用的命令有哪些?

2023-10-09
常见网络安全面试题:根据设备告警如何展开排查?

常见网络安全面试题:根据设备告警如何展开排查?

2023-10-09
常见网络安全面试题:mysql加固呢?(数据库加固)

常见网络安全面试题:mysql加固呢?(数据库加固)

2023-10-09
常见网络安全面试题:windows和linux加固?(操作系统加固)

常见网络安全面试题:windows和linux加固?(操作系统加固)

2023-10-09
在线咨询 免费试学 教程领取