How do you find odd numbers in the C language?

To obtain the sum of odd numbers, you need to loop through the given numbers, check if each number is odd, and if it is, add it to the result.

Here is an example code in C language for finding the sum of odd numbers.

#include <stdio.h>

int main() {
    int num, sum = 0;
    
    printf("请输入一系列数字,以0结束:\n");
    
    do {
        scanf("%d", &num);
        
        // 判断是否为奇数
        if (num % 2 != 0) {
            sum += num;
        }
    } while (num != 0);
    
    printf("奇数和为:%d\n", sum);
    
    return 0;
}

This sample code uses a loop to receive a series of numbers input by the user, and ends the loop when the input is 0. Within the loop, an if statement is used to determine if each number is odd, and if it is, it is added to the sum variable. Finally, the sum value is output, which represents the total sum of odd numbers.

bannerAds