easy
0 views
Escape Speed
Calculate the escape speed of an object from a planet given gravitational constant, mass, and radius
Understand the Problem
Problem Statement
The program must accept three floating point values as G (gravitational constant), M (mass) and R (radius) of a planet. The program must calculate and print the escape speed of the object with precision up to 3 decimal places.
Formula: Escape speed = √(2GM/R)
Constraints
- G, M, and R will be floating point values
- All input values will be positive
- Output must be formatted to exactly 3 decimal places
- The gravitational constant G will be in appropriate units for the calculation
Examples
Example 1
Input
1.567 2.4783 3.4671Output
1.497Explanation
Using the formula √(2GM/R): √(2 × 1.567 × 2.4783 / 3.4671) = √(7.759 / 3.4671) = √(2.237) ≈ 1.497
Example 2
Input
1.9038 2.7920 4.3937Output
1.555Explanation
Using the formula √(2GM/R): √(2 × 1.9038 × 2.7920 / 4.3937) = √(10.619 / 4.3937) = √(2.417) ≈ 1.555
Solution
#include <stdio.h>
#include <math.h>
int main() {
float g, m, r;
scanf("%f %f %f", &g, &m, &r);
float escape_speed = sqrt((2 * g * m) / r);
printf("%.3f", escape_speed);
return 0;
}Time:O(1) - Constant time calculation
Space:O(1) - Uses only a constant amount of memory
Approach:
The C solution:
- Includes necessary headers:
stdio.hfor input/output andmath.hfor thesqrt()function - Declares three float variables to store the input values
- Uses
scanf()to read the three space-separated floating point numbers - Calculates the escape speed using the formula
sqrt((2 * g * m) / r) - Prints the result formatted to 3 decimal places using
printf("%.3f", escape_speed)
Visual Explanation
Loading diagram...