如何显示小于n的所有正数偶数 [英] How to display all positive even numbers less than n

查看:196
本文介绍了如何显示小于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的所有正数偶数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆