When I run this code, I get
"The product of 5 and 2 is 10 and is odd. The sum of 5 and 2 is 7 and is odd." Which obviously is not correct since 10 is not odd, it is even. How can I fix this?
#include<iostream>
usingnamespace std;
int main ()
{
int num;
cout << "Please enter an integer: ";
cin >> num;
int num2;
cout << "Please enter another integer: ";
cin >> num2;
if ( num % 2 == 0 )
{
cout << "The product of " << num << " and " << num2 << " is " << num*num2 << " and is even." << endl;
cout << "The sum of " << num << " and " << num2 << " is " << num+num2 << " and is even." << endl;
}
else
{
cout << "The product of " << num << " and " << num2 << " is " << num*num2 << " and is odd." << endl;
cout << "The sum of " << num << " and " << num2 << " is " << num+num2 << " and is odd." << endl;
}
return (0);
}
#include<iostream>
usingnamespace std;
int main ()
{
int num;
cout << "Please enter an integer: ";
cin >> num;
int num2;
cout << "Please enter another integer: ";
cin >> num2;
int sum,product;
sum=num+num2;
product=num*num2;
if ( sum % 2 == 0 )
{
cout << "The sum of " << num << " and " << num2 << " is " << sum << " and is even." << endl;
}
else
{
cout << "The sum of " << num << " and " << num2 << " is " << sum << " and is odd." << endl;
}
if ( product % 2 == 0 )
{
cout << "The product of " << num << " and " << num2 << " is " << product << " and is even." << endl;
}
else
{
cout << "The product of " << num << " and " << num2 << " is " << product << " and is odd." << endl;
}
return (0);
}