Five sequence summation programs written in C language

1. The following is a simple sequence summation program written in C language:

#include <stdio.h>

int main() {
    int n, sum = 0;
    printf("请输入数列项数:");
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        sum += i;
    }
    printf("数列求和结果为:%d\n", sum);
    return 0;
}

In the program, we first use the scanf function to enter the number of items in the sequence from the keyboardn. Then, use a for loop to calculate the sum of the sequence and add the value of each item to the sum variable. Finally, use the printf function to output the sum of the sequence.

It should be noted that the above program assumes that the number of items in the sequencen is greater than or equal to 1. If you need to calculate the sum of other sequence, you can modify the code in the program according to the specific situation of the sequence.

2. The following is a program written in C language that can calculate the sum of a given sequence:

#include <stdio.h>

int main() {
    int n, i, sum = 0;
    printf("请输入数列项数:");
    scanf("%d", &n);
    printf("请输入数列的项:");
    for (i = 1; i <= n; i++) {
        int value;
        scanf("%d", &value);
        sum += value;
    }
    printf("数列求和结果为:%d\n", sum);
    return 0;
}

In this program, we first ask the user to enter the number of items in the sequencen, and then use the for loop to read each item of the sequence one by one, Add the value of each item to thesum variable. Finally, output the sum of the series.

You can modify the code in the program as needed to accommodate different types of sequence sums.

3. The following is a C language program that can calculate the sum of any sequence. The user can enter any number of sequence items, and the program will automatically calculate their sum:

#include <stdio.h>

int main() {
    int n, i, sum = 0;
    printf("请输入数列项数:");
    scanf("%d", &n);
    printf("请输入数列的项:");
    for (i = 1; i <= n; i++) {
        int value;
        scanf("%d", &value);
        sum += value;
    }
    printf("数列求和结果为:%d\n", sum);
    return 0;
}

In this program, we first ask the user to enter the number of items in the sequencen, and then use the for loop to read each item of the sequence one by one, Add the value of each item to thesum variable. Finally, output the sum of the series. This program can calculate the sum of any sequence of numbers, and the user can enter any number of sequence entries.

4. The following is a C language program that can calculate the sum of multiple types of arrays. The user can input items of different types of arrays as needed, and the program will automatically calculate their sum:

#include <stdio.h>

int main() {
    int n, i, sum = 0;
    printf("请输入数列项数:");
    scanf("%d", &n);
    printf("请输入数列的项:");
    for (i = 1; i <= n; i++) {
        int value;
        float num;
        char ch;
        scanf("%c", &ch);
        if (ch == '+') { // 整数数列求和
            scanf("%d", &value);
            sum += value;
        } else if (ch == '-') { // 整数数列求和,但项数不为1
            scanf("%d", &value);
            sum += value;
            n--; // 减去一个项数
        } else if (ch == '*') { // 浮点数数列求和
            scanf("%f", &num);
            sum += num;
        } else if (ch == '/') { // 浮点数数列求和,但项数不为1
            scanf("%f", &num);
            sum += num;
            n--; // 减去一个项数
        } else { // 非数字字符,结束输入
            break;
        }
    }
    printf("数列求和结果为:%d\n", sum);
    return 0;
}

In this program, we first ask the user to enter the number of items in the sequencen, and then use the for loop to read each item of the sequence one by one, Add the value of each item to thesum variable according to different character types. If the entered character is +, it means the sum of the integer sequence; if the entered character is -, it means the sum of the integer sequence, but the number of items is not 1 ; If the input character is *, it means the sum of the floating point number sequence; if the input character is /, it means the sum of the floating point number sequence, but the number of items Not 1. If non-numeric characters are entered, the input is terminated. Finally, output the sum of the series. This program can calculate the sum of multiple types of arrays, and the user can enter items of different types of arrays as needed.

5. The following is a more intelligent sequence summation program that can automatically identify the number and type of items in a sequence based on user input and calculate their sum:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int n, i, sum = 0;
    printf("请输入数列项数:");
    scanf("%d", &n);
    printf("请输入数列的项:");
    char ch;
    while ((ch = getchar()) != '\n') { // 逐个读取输入字符,直到遇到换行符
        if (ch == '+') { // 整数数列求和
            int value;
            scanf("%d", &value);
            sum += value;
        } else if (ch == '-') { // 整数数列求和,但项数不为1
            int value;
            scanf("%d", &value);
            sum += value;
            n--; // 减去一个项数
        } else if (ch == '*') { // 浮点数数列求和
            float num;
            scanf("%f", &num);
            sum += num;
        } else if (ch == '/') { // 浮点数数列求和,但项数不为1
            float num;
            scanf("%f", &num);
            sum += num;
            n--; // 减去一个项数
        } else { // 非数字字符,结束输入
            break;
        }
    }
    printf("数列求和结果为:%d\n", sum);
    return 0;
}

In this program, we use thegetchar() function to read the characters entered by the user one by one until a newline character is encountered. Depending on the input character type, use different formats to read each item of the array and add their values ​​to the sum variable. If the entered character is +, it means the sum of the integer sequence; if the entered character is -, it means the sum of the integer sequence, but the number of items is not 1 ; If the input character is *, it means the sum of the floating point number sequence; if the input character is /, it means the sum of the floating point number sequence, but the number of items Not 1. If non-numeric characters are entered, the input is terminated. Finally, output the sum of the series. This program can automatically identify the number and type of items in a sequence based on user input, and calculate their sum, making it more intelligent and flexible.

Guess you like

Origin blog.csdn.net/hudie765/article/details/134477420