Beecrowd 1014 solution || BEECROWD 1014 – Consumption Solution

Question

BEECROWD 1014 – Consumption Solution in C

Solution in C

#include<stdio.h>

int main() {

int X;
float Y;
scanf("%d%f",&X,&Y);
double a=X/Y;
printf("%.3lf km/l\n",a);//Beecrowd 1014 solution

return 0;

}

Explanation:

The code provided is a C program that calculates the fuel efficiency of the vehicle and prints kilometers per liter (km/litre). Here is a step-by-step explanation of the code.

The #include directive is used in conjunction with standard input/output libraries, which provide functions such as scanf and printf.

The main() function is the entry point of the function. Where program execution begins and ends. It has a return type of int, which means that an integer value should be returned when the program finishes execution.

The following changes are declared in the main() function.

X: Integer variable used to store the value of the distance traveled (in kilometers).

Y: A floating variable used to store fuel consumption value (in liters).

The scarf (“%d%f”, &X, &Y) statement is used to read the user input. Requires two values ​​separated by a space or other character. The first value is stored in X and the second value is stored in Y.

The next rule calculates fuel efficiency by dividing the distance traveled (X) by fuel consumption (Y). The result is stored in a double-precision variable called a.

Finally, the print (“%.3lf km/l\n”, a) statement is used to print the calculated fuel consumption. The “%.3lf” format specifier is used to print a value with three decimal places. The string “km/l” is included to indicate the unit of measurement.

sand after 0; The statement indicates the end of the main() function and indicates that the operation was successful. A value of 0 is returned to the operating system, indicating that the operation terminated without error.

In summary, this code lets the user record the distance traveled (in kilometers) and the amount of fuel used (in liters). then calculates the effect of fuel efficiency in km/l and prints the result with three decimal places.

Beecrowd 1014 solution in Python

The solution in Python:

X = int(input())
Y = float(input())
a = X / Y
print("{:.3f} km/l".format(a))

Explanation:

  1. In Python, there is no need to include any header file like in C. We can directly start writing the code.
  2. The input() function is used to read user input from the console. In Python, it automatically converts the input into a string.
  3. We read the values for X and Y using input() and convert them to the appropriate data types using int() and float() functions, respectively.
  4. The line a = X / Y performs the division operation and stores the result in the variable a.
  5. The print() function is used to display the calculated fuel efficiency. We use string formatting to specify the number of decimal places using the :.3f format specifier, which rounds the value to three decimal places.
  6. The output will be displayed on the console, showing the fuel efficiency in km/l.
  7. The return 0 statement is not necessary in Python, so it can be omitted.

Please note that Python is an interpreted language, while C is a compiled language. The code structures and syntax differ between the two languages, so it’s important to understand the specific syntax and conventions when translating code from one language to another.

Next problem: Beecrowd 1015-Distance Between Two Points solution

Leave a Comment