C语言十大经典数学应用

C语言在解决数学问题方面非常有用,因为它提供了丰富的数学函数和运算符。以下是一些经典的C语言数学题,这些题目可以帮助你提高编程和数学能力。

1. 计算圆的面积

给定圆的半径,计算圆的面积。

c 复制代码
#include <stdio.h>
#include <math.h>

int main() {
    double radius, area;
    printf("Enter the radius of the circle: ");
    scanf("%lf", &radius);
    area = M_PI * radius * radius;
    printf("The area of the circle is: %.2lf\n", area);
    return 0;
}

2. 计算阶乘

给定一个非负整数,计算其阶乘。

c 复制代码
#include <stdio.h>

int factorial(int n) {
    if (n == 0) return 1;
    return n * factorial(n - 1);
}

int main() {
    int n;
    printf("Enter a non-negative integer: ");
    scanf("%d", &n);
    printf("The factorial of %d is: %d\n", n, factorial(n));
    return 0;
}

3. 计算斐波那契数列的第N项

给定一个正整数N,计算斐波那契数列的第N项。

c 复制代码
#include <stdio.h>

int fibonacci(int n) {
    if (n <= 1) return n;
    return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
    int n;
    printf("Enter a positive integer: ");
    scanf("%d", &n);
    printf("The %dth term of the Fibonacci sequence is: %d\n", n, fibonacci(n));
    return 0;
}

4. 判断素数

给定一个正整数,判断其是否为素数。

c 复制代码
#include <stdio.h>
#include <math.h>

int isPrime(int n) {
    if (n <= 1) return 0;
    for (int i = 2; i <= sqrt(n); i++) {
        if (n % i == 0) return 0;
    }
    return 1;
}

int main() {
    int n;
    printf("Enter a positive integer: ");
    scanf("%d", &n);
    if (isPrime(n)) {
        printf("%d is a prime number.\n", n);
    } else {
        printf("%d is not a prime number.\n", n);
    }
    return 0;
}

5. 计算最大公约数

给定两个正整数,计算它们的最大公约数。

c 复制代码
#include <stdio.h>

int gcd(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

int main() {
    int a, b;
    printf("Enter two positive integers: ");
    scanf("%d %d", &a, &b);
    printf("The GCD of %d and %d is: %d\n", a, b, gcd(a, b));
    return 0;
}

6. 计算最小公倍数

给定两个正整数,计算它们的最小公倍数。

c 复制代码
#include <stdio.h>

int gcd(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

int lcm(int a, int b) {
    return a * b / gcd(a, b);
}

int main() {
    int a, b;
    printf("Enter two positive integers: ");
    scanf("%d %d", &a, &b);
    printf("The LCM of %d and %d is: %d\n", a, b, lcm(a, b));
    return 0;
}

7. 计算幂

给定底数和指数,计算幂。

c 复制代码
#include <stdio.h>
#include <math.h>

int main() {
    double base, exponent, result;
    printf("Enter the base and exponent: ");
    scanf("%lf %lf", &base, &exponent);
    result = pow(base, exponent);
    printf("The result of %.2lf raised to the power of %.2lf is: %.2lf\n", base, exponent, result);
    return 0;
}

8. 计算平方根

给定一个非负数,计算其平方根。

c 复制代码
#include <stdio.h>
#include <math.h>

int main() {
    double number, result;
    printf("Enter a non-negative number: ");
    scanf("%lf", &number);
    result = sqrt(number);
    printf("The square root of %.2lf is: %.2lf\n", number, result);
    return 0;
}

9. 计算三角函数

给定角度(以度为单位),计算其正弦、余弦和正切值。

c 复制代码
#include <stdio.h>
#include <math.h>

int main() {
    double angle, sine, cosine, tangent;
    printf("Enter the angle in degrees: ");
    scanf("%lf", &angle);
    angle = angle * M_PI / 180.0;
    sine = sin(angle);
    cosine = cos(angle);
    tangent = tan(angle);
    printf("The sine of %.2lf degrees is: %.2lf\n", angle * 180.0 / M_PI, sine);
    printf("The cosine of %.2lf degrees is: %.2lf\n", angle * 180.0 / M_PI, cosine);
    printf("The tangent of %.2lf degrees is: %.2lf\n", angle * 180.0 / M_PI, tangent);
    return 0;
}

10. 计算组合数

给定两个正整数n和r,计算组合数C(n, r)。

c 复制代码
#include <stdio.h>

int factorial(int n) {
    if (n == 0) return 1;
    return n * factorial(n - 1);
}

int combination(int n, int r) {
    return factorial(n) / (factorial(r) * factorial(n - r));
}

int main() {
    int n, r;
    printf("Enter the values of n and r: ");
    scanf("%d %d", &n, &r);
    printf("The combination C(%d, %d) is: %d\n", n, r, combination(n, r));
    return 0;
}

这些经典的C语言数学题涵盖了从基本运算到复杂函数的计算,可以帮助你提高编程和数学能力。通过解决这些问题,你可以更好地理解C语言的数学函数和运算符,以及如何将数学概念应用到编程中。

相关推荐
张书名1 小时前
Leetcode刷题记录32——搜索二维矩阵 II
算法·leetcode·矩阵
志存高远663 小时前
kotlin 扩展函数
android·开发语言·kotlin
yxc_inspire4 小时前
常见排序算法记录和理解
算法·排序
LingRannn4 小时前
【最新Python包管理工具UV的介绍和安装】
开发语言·python·uv
D_aniel_5 小时前
Leetcode:回文链表
java·算法·leetcode·链表
Sheep Shaun6 小时前
C++类与对象—下:夯实面向对象编程的阶梯
c语言·开发语言·数据结构·c++·算法
AIGC魔法师7 小时前
轮播图导航组件 | 纯血鸿蒙组件库AUI
开发语言·harmonyos·openharmony·鸿蒙开发·纯血鸿蒙·arkui / ets·鸿蒙组件库aui
后藤十八里7 小时前
Python格式化字符串的四种方法
开发语言·python·学习
10000hours7 小时前
【C语言编译】编译原理和详细过程
linux·c语言·笔记
yi个名字7 小时前
链表高级操作与算法
数据结构·算法·链表