Odd,Even and Factors

closed account (oy8URXSz)
Guys! I need some help over here. I have problem making this program.

I need to create a program that will ask the user to enter a positive integer, once the number has been entered, display all even factors and then followed by all odd factors.

Now, I only know how to display all the factors, I don't know how to seperate odd from even factors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
int main()
{
	int num,x;
	cout<<"Enter an integer: ";
	cin>>num;
	for (x=1;x<=num;x=x+1)
	{
		if (num%x==0)
			cout<<"One of the factor of integer entered is "<<x<<endl;
	}
	system("pause");
	return 0;
}
Add one more loop that defined as

for ( x = 2; x <= num; x+=2 )


And change your original loop to

for ( x = 1; x <= num; x += 2 )
Last edited on
I think you might could do it in one for loop if you change line 11 to:

cout<< x <<" is an "<<((x+2)%2?" even numer.\n":" odd numer.\n";

where x is the number, the (x+2) is so it will work with 0 and 1.

if you want to do it the assignment way though just make another for loop with the conditional being num%x==1
Last edited on
@PRW56
According to the assignment it shall be done as

display all even factors and then followed by all odd factors.

closed account (oy8URXSz)
@vlad thank you very much for your help! and @PRW56 thank you also for the effort. Thank you guys!
Topic archived. No new replies allowed.