C language programming (third edition) He Qinming exercises 5-2

C language programming (third edition) He Qinming exercises 5-2

List of exercises
1. C language programming (third edition) He Qinming exercises 2-1
2. C language programming (third edition) He Qinming exercises 2-2
3. C language programming (third edition) He Qinming exercises 2-3
4. C language programming (third edition) He Qinming exercises 2-4
5. C language programming (third edition) He Qinming exercises 2-5
6. C language programming (third edition) He Qinming exercises 2-6
7. C language programming (third edition) He Qinming exercises 3-1
8. C language programming (third edition) He Qinming exercises 3-2
9. C language programming (third edition) He Qinming exercises 3-3
10. C language programming (third edition) He Qinming exercises 3-4
11. C language programming (third edition) He Qinming exercises 3-5
12. C language programming (third edition) He Qinming exercises 4-1
13. C language programming (third edition) He Qinming exercises 4-2
14. C language programming (third edition) He Qinming exercises 4-3
15. C language programming (third edition) He Qinming exercises 4-4
16. C Language Programming (Third Edition) He Qinming with Exercises 4-5
17. C language programming (third edition) He Qinming exercises 4-6
18. C language programming (third edition) He Qinming exercises 4-7
19. C language programming (third edition) He Qinming exercises 4-8
20. C language programming (third edition) He Qinming exercises 4-9
21. C language programming (third edition) He Qinming exercises 4-10
22. C language programming (third edition) He Qinming exercises 4-11
23. C language programming (third edition) He Qinming exercises 5-1


topic

Use the function to find the odd sum:
Input a batch of positive integers (with zero or negative number as the end sign), and find the odd sum among them.
It is required to define and call the function even(n) to determine the parity of the number. When n is an even number, it returns 1; otherwise, it returns 0.
Try to write the corresponding program.


Analysis process

enter

Condition: Enter a batch of positive integers (end with zero or negative numbers)

Output

Condition: output the odd sum

Code

#include <stdio.h>
#include <math.h>
int even(int n);/*函数声明*/

int main () {
    
    
	/*定义变量*/
	int n;                                                                      /*定义变量,存储输入的正整数n*/
	int sum;                                                                    /*定义变量,计算奇数和*/
	/*赋值*/
	printf("请输入正整数n:\n");                                					/*输入提示*/
	scanf("%d \n", &n);                                            				/*输入并赋给变量*/
	
    /*计算*/
    while(n>0){
    
    /*以零或负数为结束标志,即输入的整数必须为正,才能执行,否则即停止。即一旦输入整数为负数或零时结束*/
        if(even(n)==0){
    
    
            /*输入的正整数为奇数*/
        printf("%d为奇数\n", n);                                					/*输出输入的整数为奇数的提示*/
            sum+=n;
        }    
   	    printf("请输入正整数n:\n");                                				/*输入提示*/
        scanf("%d \n", &n);                                            			/*输入下一个整数并赋给变量*/
    }
    printf("奇数和为%d\n", sum);                                        	    	/*输出奇数和*/
	return 0;
}

int even(int n){
    
    
    if(n%2==0)/*n为偶数*/ return 1;
    else/*n为奇数*/ return 0;
}

operation result

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43228814/article/details/112506814