What will be the output of the following C# code? [code] static void Main(string[] args) { int a = 4; int b = 5; int c

What will be the output of the following C# code?
Code:
 static void Main(string[] args)
 {
     int a = 4;
     int b = 5;
     int c = 6;
     int d = 8;
     if (((a * b / c) + d) >= ((b * c + d ) / a))
     {
         Console.WriteLine("Line 1 - a is greater to b");
         Console.WriteLine((a * b / c) + d);
     }
     else
     {
         Console.WriteLine("Line 1 - a is not greater to b");
         Console.WriteLine((b * c + d )/ a);
     }
 }
A.
"Line 1 - a is greater to b"
11
B.
"Line 1 - a is not greater to b"
9
C. Both are equal
D. None of the mentioned
 
Answer: a
Explanation: Now, here in ‘if’ condition both conditions of parenthesis and hence evaluating operators based on parenthesis are tested.
for expression :
Code:
 ((a * b / c) + d)
Step 1: (a*b/c) (Evaluating as 4*5/6 = 3)
Step 2: ( (a*b/c) + d ) (Evaluating (3 + 8 = 11))
Result: 11
for expression : (b * c + d )/ a
Step 1: (b*c + d) (Evaluating as 5*6 +8 = 38)
Step 2:   (b*c + d) / a (Evaluating as 38 / 4 = 9)
Result: 9
The relational operator “>=” between both expressions check for largest figure and hence consecutively executes the if condition.
Output:
Code:
         Line 1 - a is greater to b.
         11
 

Tìm kiếm

Back
Top