95.0k views
0 votes
Assume that an int variable is Quadrilateral has been declared, and that another int variable, number Of Sides has been declared and initialized. Write a statement that assigns the value 1 if number Of Sides is exactly 4 and 0 otherwise.

User VePe
by
7.0k points

1 Answer

5 votes

Answer:

The answer to this question can be given as:

Statement:

isQuadrilateral = (numberOfSides == 4) ? 1 : 0;

//check condition using ternary operator.

Explanation:

we know that both(Quadrilateral, numberOfSides) is already declared in the program. So the statement for check condition is (isQuadrilateral = (numberOfSides == 4) ? 1 : 0;). To check this condition we use the ternary operator. In this operator, we also check another condition. The syntax of ternary operator (condition ? value_if_true : value_if_false).In this statement on the lift side we use the variable for check condition and right side we check condition if the value is true it prints 1 else it will print 0.

User Dotdotcommadot
by
8.2k points