如何显示小于n的所有正数偶数

编程入门 行业动态 更新时间:2024-10-25 10:27:33
本文介绍了如何显示小于n的所有正数偶数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用for语句,编写一个程序,提示用户输入正整数n,然后显示所有小于n的正数偶数,然后显示它们的总和和产品。 请告诉我如何显示偶数号码

Using for statement, write a program that prompts the user for a positive integer n then display all positive even numbers less than n then display their sums and product. Please tell me how to display even number

#include <iostream> using namespace std; int main() { int n; int sum; int product=1; int countNumber = 0; cout << "Please enter the number of positive integers = "; cin >> n; if (n > 0) { for (sum = 0;n > countNumber;) { sum = sum + n; product = product * n; n = n - 1; } } cout << "The sum the integers number = " << sum << endl; cout << " The product number of integers = " << product << endl; system("pause"); return 0; }

推荐答案

请看我对这个问题的评论。 首先,偶数是 N 的数字,其中 N%2 == 0 ;第二,具有偶数 N ,您可以获得下一个偶数,即 N + 2 ,其中会让你使用一些从零开始的循环。这些考虑因素,你应该首先自己进行,应该足以让你写出这段琐碎的代码。现在轮到你了。
-SA
Please see my comment to the question. First, an even number is the number N with N % 2 == 0; second, having an even number N, you can obtain "next" even number, which is N + 2, which will let you to use some loop starting from zero. These considerations, which you should have conducted by yourself in first place, should be enough for you to write this trivial piece of code. Now it's your turn.
—SA

#include <iostream> using namespace std; int main() { int n; int sum=0; int product = 1; int countNumber = 0; cout << "Please enter the number of positive integers = "; cin >> n; for (countNumber = 2; n>countNumber; countNumber += 2) // everything in the for(;;) { cout << countNumber << endl; sum = sum + countNumber; product = product * countNumber; } { cout << " The even Numbers are = " << n << endl; cout << "The sum of the integers number = " << sum << endl; cout << " The product number of integers = " << product << endl; } system("pause"); return 0; }</iostream>

更多推荐

如何显示小于n的所有正数偶数

本文发布于:2023-11-29 23:04:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647897.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正数   偶数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!