c++ - What should I replace num with? -
i'm trying write program determine whether both sum , product of 2 integers either or odd.
everything seems fine, except when run program, comes out: "the product of 2 , 3 5 , even." even?
why when should odd? understanding, reading num
since put (num%2==0)
, that's why saying even. how can make read outcome of 2 numbers (sum/product)?
#include<iostream> using namespace std; int main () { int num; cout << "please enter integer: "; cin >> num; int num2; cout << "please enter integer: "; cin >> num2; if ( num % 2 == 0 ) { cout << "the product of " << num << " , " << num2 << " " << num*num2 << " , even." << endl; cout << "the sum of " << num << " , " << num2 << " " << num+num2 << " , even." << endl; } else { cout << "the product of " << num << " , " << num2 << " " << num*num2 << " , odd." << endl; cout << "the sum of " << num << " , " << num2 << " " << num+num2 << " , odd." << endl; } return (0); }
you checking if num
even. instead, should calculate product , sum, , check them both:
int sum = num + num2; int product = num * num2; if (sum % 2 == 0 && product % 2 == 0) { cout << sum , product both << endl; } else { cout << sum , product not both << endl; }
Comments
Post a Comment