There are 3 integer variables which can have values 0 or 1. If all are 0 or all are 1, print a particular statement. For all other combination of values print another statement.
I tried the following that works. Is there a better way to write the if statement?
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
cin >> a >> b >> c;
if(!(a != 0 && b != 0 && c != 0) && !(a == 0 && b == 0 && c == 0))
{
cout << "a, b or c have mixed values of 1 and 0" << endl;
}
else
{
cout << "All of a, b and c are either 1 or 0" << endl;
}
system("pause");
return 0;
}
Sorry to have caused some confusion. Actually there is no check on the values of a,b & c imposed in the above code, since I gave it as a simple example. The if statement is not to check whether a, b & c are all equal or not. It is to check if all of them are 0 or 1 integer values (not boolean).
1 Answer