Given four integer variables a, b, c, and d, write a single expression to evaluate the following formula:
Use integer division while evaluating the expression.
Examples:
Input: a = 10, b = 4, c = 7, d = 9
Output: 11
Explanation: 10 + 4 = 14, 14 / 7 = 2 and 2 + 9 = 11.Input: a = 5, b = 6, c = 8, d = 9
Output: 10
Explanation: 5 + 6 = 11, 11 / 8 = 1(integer division) and 1 + 9 = 10.
Try It Yourself
The key idea is very simple: directly translate the given mathematical formula into a programming expression.
Since all inputs are integers and integer division is required, the division operation should be performed using integer operands so that the fractional part is discarded.
- Read the four integer values a, b, c, and d.
- Substitute these values into the given formula.
- Use the corresponding arithmetic operators to construct the expression.
- Perform the division using integer arithmetic and print the resulting value.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a, b, c, d;
// cin >> a >> b >> c >> d;
a = 10;
b = 4;
c = 7;
d = 9;
// Calculate the expression using integer division.
int res = ((a + b) / c) + d;
cout << res << endl;
return 0;
}
#include <stdio.h>
int main()
{
int a = 10;
int b = 4;
int c = 7;
int d = 9;
// Calculate the expression using integer division.
int res = ((a + b) / c) + d;
printf("%d\n", res);
return 0;
}
class GFG {
public static void main(String[] args)
{
int a = 10;
int b = 4;
int c = 7;
int d = 9;
// Calculate the expression using integer division.
int res = ((a + b) / c) + d;
System.out.println(res);
}
}
# Driver Code
if __name__ == "__main__":
a = 10
b = 4
c = 7
d = 9
# Calculate the expression using integer division.
res = ((a + b) // c) + d
print(res)
using System;
class GFG {
public static void Main(string[] args)
{
int a = 10;
int b = 4;
int c = 7;
int d = 9;
// Calculate the expression using integer division.
int res = ((a + b) / c) + d;
Console.WriteLine(res);
}
}
// Driver Code
let a = 10;
let b = 4;
let c = 7;
let d = 9;
// Calculate the expression using integer division.
let res = Math.floor((a + b) / c) + d;
console.log(res);
Output
11