打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c语言练习实例01
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
输入两个正整数m和n,求其最大公约数和最小公倍数;
输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数
求Sn a+aa+aaa+aaaa+......aa..a(n个a)的值,其中a是一个数字,n表示a的位数
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー

1、输入两个正整数m和n,求其最大公约数和最小公倍数;

#include<stdio.h>
#include<math.h>

//输入两个正整数m和n,求其最大公约数和最小公倍数;
//方法:辗转相除法

int main()
{
int m, n; //两个正整数m和n
int p, k; //最小公倍数,余数
printf("please enter two positive integers: \n");
scanf("%d,%d",&m,&n);
p = m * n; 
while (k = m%n){
m = n;
n = k;
}
p = p / n;

printf("最大公约数是:%d\n",n);
printf("最小公倍数是:%d\n",p);

return 0;
}

結果:
please enter two positive integers:
3,6
最大公约数是:3
最小公倍数是:6

2、输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数

#include<stdio.h>
#include<math.h>

//输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数

int main()
{
char c;
int letters = 0, spaces = 0, digitals = 0, others = 0;
printf("输入一行字符:");
while ((c = getchar())!= '\n'){
if (c>='a'&& c<='z' || c>='A' && c <= 'Z'){
letters++;
}
else if (c == ' '){
spaces++;
}
else if (c >= '0' && c <= '9'){
digitals++;
}
else
{
others++;
}
}

printf("the number of letters:%d\n",letters);
printf("the number of spaces:%d\n",spaces);
printf("the number of digitals:%d",digitals);
printf("the number of others:%d",others);

return 0;
}

3、//求Sn a+aa+aaa+aaaa+......aa..a(n个a)的值,其中a是一个数字,n表示a的位数,例如
//2+22+222+2222+22222(此时a5)
//n由键盘输入

#include<stdio.h>
#include<math.h>

int main()
{
int a, n,sum=0;
printf("please enter the number of a and n:");
scanf("%d,%d",&a,&n);
for (int i = 0; i < n;i ++){
sum += a;
a = a * 10 + a;
}
printf("the sum=%d\n", sum);
return 0;
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
江苏计算机等级考试C语言考前冲刺模拟试卷
求最大公约数和最小公倍数,自顶向下,逐步求精的设计方法,函数
C经典问题整理(一)(素数,公倍公约数,斐波那契数列)
函数典型程序
C语言10个经典小程序——小白必备!
C|模数、素数,辗转相除法的证明及求最大公约数和最小公倍数
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服