Answers to Tan Haoqiang's C language design exercises (1)

Table of contents

Input 10 numbers in sequence and ask to output the largest number among them.

There are three numbers a, b, c, and they are required to be output in order of size.

Find 1+2+3+……100

Determine whether a number n is divisible by both 3 and 5

Output prime numbers between 100 and 200


Input 10 numbers in sequence and ask to output the largest number among them.


Following is a program written in C language to input 10 numbers and find the maximum among them:
 

#include <stdio.h>

int main() {
    int i;
    double numbers[10]; // 声明一个包含10个元素的数组,用于存储输入的数字

    // 输入10个数字
    printf("请输入10个数字:\n");
    for (i = 0; i < 10; i++) {
        printf("请输入第 %d 个数字:", i + 1);
        scanf("%lf", &numbers[i]);
    }

    // 初始化最大值为第一个数字
    double max = numbers[0];

    // 找出最大值
    for (i = 1; i < 10; i++) {
        if (numbers[i] > max) {
            max = numbers[i];
        }
    }

    // 输出最大值
    printf("最大的数是:%.2lf\n", max);

    return 0;
}

This C code will ask you to enter 10 numbers and then find the maximum value among the entered numbers and print it to the screen. Note that I use the `%lf` format specifier to read floating point input.

There are three numbers a, b, c, and they are required to be output in order of size.


The following is a program written in C language to output three numbers a, b, c in order of size:
 

#include <stdio.h>

int main() {
    double a, b, c;

    // 输入三个数
    printf("请输入三个数:\n");
    printf("请输入第一个数:");
    scanf("%lf", &a);
    printf("请输入第二个数:");
    scanf("%lf", &b);
    printf("请输入第三个数:");
    scanf("%lf", &c);

    // 比较并按大小顺序输出
    if (a <= b && a <= c) {
        printf("按大小顺序输出:%.2lf, ", a);
        if (b <= c) {
            printf("%.2lf, %.2lf\n", b, c);
        } else {
            printf("%.2lf, %.2lf\n", c, b);
        }
    } else if (b <= a && b <= c) {
        printf("按大小顺序输出:%.2lf, ", b);
        if (a <= c) {
            printf("%.2lf, %.2lf\n", a, c);
        } else {
            printf("%.2lf, %.2lf\n", c, a);
        }
    } else {
        printf("按大小顺序输出:%.2lf, ", c);
        if (a <= b) {
            printf("%.2lf, %.2lf\n", a, b);
        } else {
            printf("%.2lf, %.2lf\n", b, a);
        }
    }

    return 0;
}

This program will ask you to enter three numbers, then it will compare the numbers and output them to the screen in ascending order.

Find 1+2+3+……100


To calculate 1+2+3+...+100, you can write a loop in C language to accumulate the numbers. Here is an example program:
 

#include <stdio.h>

int main() {
    int sum = 0;
    int i;

    for (i = 1; i <= 100; i++) {
        sum += i;
    }

    printf("1+2+3+...+100 = %d\n", sum);

    return 0;
}

Determine whether a number n is divisible by both 3 and 5


This program will initialize a variable `sum` to 0, then use a `for` loop to increase from 1 to 100, accumulating each number into `sum`. Finally, it prints the accumulated results.

#include <stdio.h>

int main() {
    int n;

    // 输入待判断的数
    printf("请输入一个整数:");
    scanf("%d", &n);

    // 判断是否同时被3和5整除
    if (n % 3 == 0 && n % 5 == 0) {
        printf("%d 同时被3和5整除。\n", n);
    } else {
        printf("%d 不同时被3和5整除。\n", n);
    }

    return 0;
}

Output prime numbers between 100 and 200


The following is a C language program that prints all prime numbers between 100 and 200:
 

#include <stdio.h>

// 函数用于判断一个数是否是素数
int isPrime(int num) {
    if (num <= 1) {
        return 0; // 1和负数不是素数
    }
    for (int i = 2; i * i <= num; i++) {
        if (num % i == 0) {
            return 0; // 如果能整除,则不是素数
        }
    }
    return 1; // 是素数
}

int main() {
    int lower = 100;
    int upper = 200;

    printf("100到200之间的素数有:\n");

    for (int num = lower; num <= upper; num++) {
        if (isPrime(num)) {
            printf("%d ", num);
        }
    }

    printf("\n");

    return 0;
}

This program defines an `isPrime` function, which is used to determine whether a number is prime. It then iterates through each number from 100 to 200 in the main function, calls the `isPrime` function on each number to check if it is a prime number, and outputs the prime number to the screen.

Find the greatest common divisor of two numbers m and n


To find the greatest common divisor (GCD) of two numbers m and n, you can calculate it using Euclidean's algorithm. The following is an example of a C language program:

#include <stdio.h>

// Function used to calculate the greatest common divisor
int gcd(int m, int n) {     if (n == 0) {         return m;     }     return gcd(n, m % n); }




int main() {
    int m, n;

    // Enter two integers
    printf("Please enter two integers m and n:");
    scanf("%d %d", &m, &n);

    // Call the gcd function to calculate the greatest common divisor
    int result = gcd(m, n);

    //Output result
    printf("The greatest common divisor is: %d\n", result);

    return 0;
}

This program first asks the user to enter two integers m and n, then calls a function called `gcd` to calculate their greatest common divisor and prints the result to the screen. The `gcd` function uses recursion to calculate the greatest common divisor until one of the numbers becomes zero, at which point the other number becomes the greatest common divisor.

Guess you like

Origin blog.csdn.net/qq_50942093/article/details/132952501