Precedence Puzzle

Precedence Puzzle

Easy C Operators 27 views
Explanation Complexity

Problem Statement

Write the expression: a = 5, b = 10, c = 15; result = a + b * c / 5 - 2 % 3; calculate manually, following operator precedence. Then verify with the program.

Input Format

a = 5
b = 10
c = 15
result = a + b * c / 5 - 2 % 3

Output Format

Value of result.

Example

a = 5, b = 10, c = 15
result = 33

Constraints

• Integer arithmetic

• Standard C operator precedence rules apply

Concept Explanation

The expression is evaluated using operator precedence and left-to-right associativity.

Step-by-Step Explanation

1.Substitute values:
result = 5 + 10 * 15 / 5 - 2 % 3

2.Evaluate *, /, % first (left to right):

• 10 * 15 = 150

• 150 / 5 = 30

• 2 % 3 = 2

3.Expression becomes:
5 + 30 - 2

4.Evaluate + and -:

• 5 + 30 = 35

• 35 - 2 = 33

5.Final value stored in result is 33.

Concept Explanation

The expression is evaluated using operator precedence and left-to-right associativity.

Step-by-Step Explanation

1.Substitute values:
result = 5 + 10 * 15 / 5 - 2 % 3

2.Evaluate *, /, % first (left to right):

• 10 * 15 = 150

• 150 / 5 = 30

• 2 % 3 = 2

3.Expression becomes:
5 + 30 - 2

4.Evaluate + and -:

• 5 + 30 = 35

• 35 - 2 = 33

5.Final value stored in result is 33.

Input / Output Format

Input Format
a = 5
b = 10
c = 15
result = a + b * c / 5 - 2 % 3
Output Format
Value of result.
Constraints
• Integer arithmetic

• Standard C operator precedence rules apply

Examples

Input:
a = 5, b = 10, c = 15
Output:
result = 33

Example Solution (Public)

C
#include <stdio.h>

int main() {
    int a = 5, b = 10, c = 15;
    int result;

    result = a + b * c / 5 - 2 % 3;

    printf("Result = %dn", result);

    return 0;
}

Official Solution Code

#include <stdio.h>

int main() {
    int a = 5, b = 10, c = 15;
    int result;

    result = a + b * c / 5 - 2 % 3;

    printf("Result = %dn", result);

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.