Problem Statement
Alexa has a drink that has A kilocalories of energy per 100 milliliters. How many kilocalories of energy does B milliliters of this drink have?
Input
The input consists of two space separated integers A and B.
Constraints
0 ≤ A, B ≤1000
All values in input are integers.
Constraints
0 ≤ A, B ≤1000
All values in input are integers.
Output
Print a number representing the answer up to 2 decimal places.
Your output is considered correct when its absolute or relative error from our answer is at most 10−2.
Your output is considered correct when its absolute or relative error from our answer is at most 10−2.
Approach:-
In above problem statement there is an drink. On that drink per 100 milliliters there is A kilocalories are given. We have given two inetgers that is A and B and A is for kilocalories and B is for the value of drink in milliliters. So we have to find in drink B how many kilocalories are present.
Suppose we have given A=30 and B=200 so in this for every 100 milliliters we have given 30 kilocalories. For 100 it is 30 so for 200 it is 30+30 = 60. So drink B has total 60 kilocalories present.
So to solve this problem you use any of the below approach:-
1) You can divide A by 100 first like - (A / 100.0) and then you can multiply them with B.
Total Kilocalories = (A / 100.0) * B
2) You can divide B by 100 first like - (B / 100.0) and then you can multiply them with A.
Total Kilocalories = (B / 100.0) * A
3) You can multiply both A and B first like - ( A * B) and then you can divide them with 100.0.
Total Kilocalories = (A * B) / 100.0
These are the three ways you can solve this problem.
For Example:-
A = 45 B = 200
First, divide A by 100.0 so - 45/100.0 = 0.45
Then, multiply it with 200 so - 0.45 * 200 = 90 but we have to print our output till 2 decimal places as per problem statement. So the output should be = 90.00
Solution:-
Java Code:-
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
// Your code here
Scanner sc=new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
double result = (b / 100.0) * a;
System.out.printf("%.2f", result);
}
}
C++ Code:-
#include <bits/stdc++.h> // header file includes every Standard library
using namespace std;
int main() {
// Your code here
int B ,A;
cin >> A >> B ;
float c = (A*B/100.0) ;
cout << c ;
return 0;
}
JavaScript Code:-
// Your code here
const input = require('fs').readFileSync('/dev/stdin', 'utf8');
const [A, B] = input.split(' ')
console.log(A*B/100);
Python Code:-
# Your code here
A,B = map(int,input().split())
ans = A*B/100
print(ans)
I have tried to given the solution of this problem in C++, Java, JavaScript and Python. You need to do some changes in this code.
I you like this article the subscribe and share it with your friends.
No comments: