In: Computer Science
Which expression can be used to check if an integer x is a multiple of 5 within the range 20 to 50 inclusively?
Select one:
a. (x >= 20 && x % 5 == 0) || (x <= 50 && x % 5 == 0)
b. x <= 50 && x >= 20 && x % 5 == 0
c. 20 <= (x % 5) <= 50
d. (x % 5 == 0) && (20 <= x <= 50)
Answer B is correct
x <= 50 && x >= 20 && x % 5 == 0
Explanation:
#include <iostream>
using namespace std;
int main()
{
int x;
cout<<"enter a number"<<endl;
cin>>x;
if(x <= 50 && x >= 20 && x % 5 == 0){
// this expression is true for all multiple of 5
cout<<"expression give true result"<<endl;
}
else{
cout<<"expression give false result"<<endl;
}
return 0;
}
Option B :Answer option
Output:1 (for 15 it gives false result because it is less than 20)
Output 2:(for 25 it give true because it is a multiple of 5 and in a range of 20 and 50)
Option A:(x >= 20 && x % 5 == 0) || (x <= 50 && x % 5 == 0)(Wrong)
Proof:This expression give true for all values which give x%5==0
Opction C: 20 <= (x % 5) <= 50 (wrong)
Proof: This expression give true for all values of x
Option D: (x % 5 == 0) && (20 <= x <= 50) (Wrong)
Proof: This expression give true for all values which give x%5==0