
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 à hAfter 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 à hAfter 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
- First, define variables for mass (m), height (h), and gravitational acceleration (g = 9.8).
- Now, use the formula: Potential Energy = m à g à h to calculate the potential energy.
- 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 JTime 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
- Define a function calculatePotentialEnergy() to compute potential energy.
- Pass the mass and height as input arguments to the function.
- Use the formula Potential Energy = m à g à h inside the function.
- 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 JTime 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.