In: Computer Science
Answer these using VISUAL STUDIOS not CTT or PYTHON
1.Write an expression that evaluates to true if the value of the integer variable x is divisible (with no remainder) by the integer variable y. (Assume that y is not zero.)
2.Write an expression that evaluates to true if the value of the integer variable numberOfPrizes is divisible (with no remainder) by the integer variable numberOfParticipants. (Assume that numberOfParticipants is not zero.)
3.Write an expression that evaluates to true if the integer variable x contains an even value, and false if it contains an odd value.
4.Given the integer variables x and y, write a fragment of code that assigns the larger of x and y to another integer variable max.
We can make use of conditional operator(ternary operator) of solving all of the above four questions.
Conditional operator is of the form
condition ? statement 1 : statement 2
If condition is true statement 1 is executed else statement 2 is executed
1)
bool a = x % y == 0 ? true : false ;
Here a is boolean variable that will store true if x is divisible by y without leaving any remainder
2)
bool b = numberOfPrizes % numberOfParticipants == 0 ? true : false ;
Now b is also a boolean variable that will store true if numberOfPrizes is divisible by numberOfParticipants without leaving any remainder
3)
bool c = x % 2 == 0 ? true : false ;
As we know an even number is divisible by 2 without leaving any remainder.Therefore will check if on dividing by 2 no remainder is present, then x is even else odd and c is boolean variable that will store the output.
4)
int max = x>=y ? x : y ;
In this expression instead of using boolean variable we have used integer value as we have to store value in max. Here x>=y condition will be checked and if conditon is true statement 1 i.e. x will be returned to max else statement2 i.e. y will be stored in max.