Calculate Potential Energy in C++



What is Potential Energy?

Potential energy is a type of stored energy stored by an object due to its position relative to other objects. It is an important concept in physics and is commonly calculated when studying objects in gravitational fields.

In this tutorial, we are going to learn how to calculate the potential energy of an object in C++ if the mass and height are given.

Formula for Calculating Potential Energy

The formula for potential energy is as follows:

Potential Energy = m × g × h

Where:
m is the mass of the object (in kilograms).
g is the gravitational acceleration ( 9.8 m/s² on Earth).
h is the height of the object from the reference point (in meters).

Example 1

  • Input:
    Mass (m) = 10 kg
    Height (h) = 5 m
  • Output: 490 J (Joules)

Explanation:

Using the formula for potential energy: PE = m × g × h
After substituting values:
PE = 10 × 9.8 × 5 = 490J

Example 2

  • Input:
    Mass (m) = 0 kg
    Height (h) = 10 m
  • Output: 0 J

Explanation:

If the mass of the object is zero, there is no object to store potential energy, so the potential energy is 0 J.

Example 3

  • Input:
    Mass (m) = 15 kg
    Height (h) = 0 m
  • Output: 0 J

Explanation:

If the height of the object is zero, the object is on the ground. Since potential energy depends on height above the ground, no potential energy is stored. Therefore, the potential energy is 0 J.

Example 4

  • Input:
    Mass (m) = 15 kg
    Height (h) = 3 m
  • Output: 441 J (Joules)

Explanation:

Using the formula for potential energy: PE = m × g × h
After substituting values:
PE = 15 × 9.8 × 3 = 441J

Calculating Potential Energy Using Direct Formula Approach

In this approach, we calculate the potential energy using the formula directly in the program. We first take mass and height as input parameters. Now, calculate the potential energy if mass and height are given using the formula: Potential Energy = m × g × h. After calculating, print the result.

Steps for Implementation

  1. First, define variables for mass (m), height (h), and gravitational acceleration (g = 9.8).
  2. Now, use the formula: Potential Energy = m × g × h to calculate the potential energy.
  3. Output the result.

Implementation Code

#include<iostream>
using namespace std;

int main() {
    double mass = 10.0; // in kilograms
    double height = 5.0; // in meters
    const double gravity = 9.8; // in m/s²

    // calculate potential energy using formula
    double potentialEnergy = mass * gravity * height;

    // Output
    cout << "The potential energy of an object with a mass of " << mass
    << " kg at a height of " << height << " m is: "
    << potentialEnergy << " J" << endl;

    return 0;
}

Output:

The potential energy of an object with a mass of 10 kg at a height of 5 m is: 490 J
Time Complexity: O(1)
Space Complexity: O(1)

Calculating Potential Energy Using Function

In this approach, we create a function to calculate potential energy. This makes the program reusable and modular. We will use a function to calculate the potential energy. The logic and formula for calculating the potential energy are the same. We will use a function to reuse code later by simply changing the value.

Steps for Implementation

  1. Define a function calculatePotentialEnergy() to compute potential energy.
  2. Pass the mass and height as input arguments to the function.
  3. Use the formula Potential Energy = m × g × h inside the function.
  4. Return the result and print it.

Implementation Code

#include<iostream>
using namespace std;

// Function to calculate potential energy
double calculatePotentialEnergy(double mass, double height) {
    const double gravity = 9.8; // in m/s²
    return mass * gravity * height;
}

int main() {
    // Input values for mass and height
    double mass = 10.0; // in kilograms
    double height = 5.0; // in meters

    // Call the function to calculate potential energy
    double potentialEnergy = calculatePotentialEnergy(mass, height);

    // Output
    cout << "The potential energy of an object with mass " << mass
    << " kg at a height of " << height << " m is: "
    << potentialEnergy << " J" << endl;

    return 0;
}

Output:

The potential energy of an object with a mass of 10 kg at a height of 5 m is: 490 J
Time Complexity: O(1)
Space Complexity: O(1)

Applications of Potential Energy

  • Potential energy stored in reservoirs is converted into kinetic energy which is further used for hydroelectric power generation.
  • Potential energy is used in spring systems, pendulums, and other mechanical setups.
Updated on: 2025-01-20T19:10:49+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements