1、5!怎么求?
#include<stdio.h>
int main()
{
int t, i;
t = 1;
i = 2;
while (i <= 5)
{
t = t * i;
i++;
}
printf("5! = %d", t);
return 0;
}
2、求多项式1-1/2+1/3.....-1/100的值
#include<stdio.h>
int main()
{
int sign = 1;
double deno = 2.0, sum = 1.0, term;
while (deno <= 100)
{
sign = -sign;
term = sign / deno;
sum = sum + term;
deno++;
}
printf("%f\n", sum);//%f以浮点型输出
return 0;
}
3、对数运算的奇妙
#include<stdio.h>
#include<math.h>
int main()
{
double d = 300000, p = 6000, r = 0.01, m;
m = log(p / (p - d * r)) / log(1 + r);//log表示ln 是以e为底数的
printf("m = %4.1f\n", m);//4是指数值的宽度 1是指小数点后的位数
return 0;
}
4.有点问题
#include<stdio.h>
#include<math.h>
int main()
{
int a, b;
float x, y;
char c1, c2;
scanf_s("%d %d", &a, &b);
scanf_s("%f%f", &x, &y);
scanf_s("%c", &c1);
scanf_s("%c", &c2);
printf("a=%d,b=%d,x=%f,y=%f,c1=%c,c2=%c", a, b, x, y, c1, c2);
return 0;
}
5.猜数字-随机数种子
#include<stdio.h>
#include<stdlib.h>//为了使用srand() 设置随机数的种子
#include<time.h>//为了使用time() 获取当前的系统时间
int main()
{
srand(time(0));//设置随机数种子
int number = rand() % 100 + 1;//%100 取后随机数的后两位
int count = 0;
int a = 0;
printf("请输入一个1~100的数字\n");
do
{
scanf_s("%d", &a);
count++;
if (a > number)
{
printf("你猜的数过大\n");
}
else if(a<number)
{
printf("你猜的数过小\n");
}
} while (a != number);
printf("恭喜你猜到此数为%d\n", a);
printf("用%d次", count);
return 0;
}
6.整数逆序
#include<stdio.h>
int main()
{
int x, digit, ret = 0;
scanf_s("%d", &x);
while (x > 0)
{
digit = x % 10;
//printf("%d\n", digit);
ret = ret * 10 + digit;
printf("x=%d,digit=%d,ret=%d\n", x, digit, ret);
x /= 10;
}
printf("%d", ret);
return 0;
}
7、整数正序
#include<stdio.h>
int main()
{
//若x=1253
int x;
scanf_s("%d", &x);
//区分t与x
int t = x;
//求mask

文章展示了C语言实现的多个数学计算示例,包括阶乘、求和、对数运算、整数逆序排列、正序排列、最大公约数、无重复数字的3位数生成、水仙花数和素数判断。这些例子涵盖了基础的循环结构、数学逻辑和算法应用。
最低0.47元/天 解锁文章
2350

被折叠的 条评论
为什么被折叠?



