Open In App

Program to calculate Kinetic Energy and Potential Energy

Improve
Improve
Like Article
Like
Save
Share
Report

Given three float values M, H, and V representing the mass, velocity, and height of an object respectively the task is to calculate its Kinetic Energy as well as its Potential Energy
Note: The value of acceleration due to gravity (g) is 9.8 and ignore units.   

Examples:

Input: M = 25, H = 10, V = 15
Output:
Kinetic Energy = 2812.5
Potential Energy = 2450
Explanation : The kinetic energy of the particle is 2812.5 and the potential energy is 2450.

Input : M=5.5, H=23.5, V= 10.5
Output :
303.188
1266.65

 

Approach: The required values of Kinetic Energy and Potential Energy can be calculated using the following two formulas:

Kinetic Energy = 0.5 * Mass ( M ) * Velocity ( V ) 2 

Potential Energy = Mass ( M ) * Height ( H ) * Acceleration due to gravity ( g )

Below is the implementation of the above approach:

C++14




// C++ Program to implement
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate Kinetic Energy
float kineticEnergy(float M, float V)
{
    // Stores the Kinetic Energy
    float KineticEnergy;
 
    KineticEnergy = 0.5 * M * V * V;
 
    return KineticEnergy;
}
 
// Function to calculate Potential Energy
float potentialEnergy(float M, float H)
{
    // Stores the Potential Energy
    float PotentialEnergy;
 
    PotentialEnergy = M * 9.8 * H;
 
    return PotentialEnergy;
}
 
// Driver Code
int main()
{
    float M = 5.5, H = 23.5, V = 10.5;
 
    cout << "Kinetic Energy = "
         << kineticEnergy(M, V) << endl;
    cout << "Potential Energy = "
         << potentialEnergy(M, H) << endl;
    return 0;
}


Java




// Java program to implement
// the above approach
class GFG{
  
// Function to calculate Kinetic Energy
static double kineticEnergy(double M, double V)
{
     
    // Stores the Kinetic Energy
    double KineticEnergy;
 
    KineticEnergy = 0.5 * M * V * V;
 
    return KineticEnergy;
}
 
// Function to calculate Potential Energy
static double potentialEnergy(double M, double H)
{
     
    // Stores the Potential Energy
    double PotentialEnergy;
 
    PotentialEnergy = M * 9.8 * H;
 
    return PotentialEnergy;
}
 
// Driver Code
public static void main(String []args)
{
    double M = 5.5, H = 23.5, V = 10.5;
 
    System.out.println("Kinetic Energy = " +
                       kineticEnergy(M, V));
    System.out.println("Potential Energy = " +
                       potentialEnergy(M, H));
}
}
 
// This code is contributed by AnkThon


Python3




# Python3 program to implement
# the above approach
 
# Function to calculate Kinetic Energy
def kineticEnergy(M, V):
 
    # Stores the Kinetic Energy
    KineticEnergy = 0.5 * M * V * V
 
    return KineticEnergy
 
# Function to calculate Potential Energy
def potentialEnergy(M, H):
 
    # Stores the Potential Energy
    PotentialEnergy = M * 9.8 * H
 
    return PotentialEnergy
 
# Driver Code
if __name__ ==  "__main__":
 
    M = 5.5
    H = 23.5
    V = 10.5
 
    print("Kinetic Energy = ", kineticEnergy(M, V))
    print("Potential Energy = ", potentialEnergy(M, H))
     
# This code is contributed by AnkThon


C#




// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
 
class GFG{
  
/// Function to calculate Kinetic Energy
static double kineticEnergy(double M, double V)
{
     
    // Stores the Kinetic Energy
    double KineticEnergy;
 
    KineticEnergy = 0.5 * M * V * V;
 
    return KineticEnergy;
}
 
// Function to calculate Potential Energy
static double potentialEnergy(double M, double H)
{
     
    // Stores the Potential Energy
    double PotentialEnergy;
 
    PotentialEnergy = M * 9.8 * H;
 
    return PotentialEnergy;
}
 
// Driver Code
public static void Main()
{
    double M = 5.5, H = 23.5, V = 10.5;
 
    Console.WriteLine("Kinetic Energy = " +
                      kineticEnergy(M, V));
    Console.Write("Potential Energy = " +
                  potentialEnergy(M, H));
}
}
 
// This code is contributed by bgangwar59


Javascript




<script>
 
      // Javascript program for the above approach
 
      // Function to calculate Kinetic Energy
        function kineticEnergy(M, V)
        {
          // Stores the Kinetic Energy
            let KineticEnergy;
 
            KineticEnergy = 0.5 * M * V * V;
 
            return KineticEnergy;
        }
 
        // Function to calculate Potential Energy
        function potentialEnergy(M, H) {
            // Stores the Potential Energy
            let PotentialEnergy;
 
            PotentialEnergy = M * 9.8 * H;
 
            return PotentialEnergy;
        }
 
        // Driver Code
 
        let M = 5.5, H = 23.5, V = 10.5;
 
        document.write("Kinetic Energy = "
            + kineticEnergy(M, V))
         
        document.write("<br>");
         
        document.write( "Potential Energy = "
            + potentialEnergy(M, H))
 
        // This code is contributed by Hritik
         
 </script>


Output: 

Kinetic Energy = 303.188
Potential Energy = 1266.65

 

Time Complexity: O(1)
Auxiliary Space: O(1)

 



Last Updated : 12 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads