Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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

C++
#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;
}
Posted
Comments
Sergey Alexandrovich Kryukov 1-Nov-15 16:29pm    
How? By thinking just a bit.
—SA
Assam ALzookery 1-Nov-15 16:49pm    
can you tell me where's my mistake


#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 % 2 == 0) {
for (sum = 0; n >= 0;)

{

sum = sum + n;
product = product * n;
n = n + 2;
}
}
cout << "The even Numbers are = " << n << endl;
cout << "The sum the integers number = " << sum << endl;
cout << " The product number of integers = " << product << endl;

system("pause");
return 0;
}
Sergey Alexandrovich Kryukov 1-Nov-15 17:11pm    
No valid loop. Should be three terms separated by ';': starting initialization, end condition, step statement.
Don't checkup n%2, instead, calculate new ending value valid no matter if initial value is odd or even. Use different loop variable and declare it in loop statement (not only initialize, declare), and never modify it inside loop.

In other words, you need to get familiar with loops from ground zero.

—SA
Matthew Dennis 1-Nov-15 17:45pm    
You too.
An empty iteration statement (the third part) is a valid expression. This can be zero or more expressions separated by the comma operator, and don't have to do anything related to the termination condition.
Sergey Alexandrovich Kryukov 1-Nov-15 20:57pm    
You are right. I don't think it would make much sense in this particular case, but yes, it's possible to implement things without this third term. Anyway, I would advise to master the most widely used 3-term form of "for" loop before experimenting with more specialized forms.

Thank you for this comment.

—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
 
Share this answer
 
C++
#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>
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 1-Nov-15 20:59pm    
Better, but this is hardly a "solution". And why would you self-accept it formally?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900