Program to calculate Kinetic Energy and Potential Energy - GeeksforGeeks

12 Jul, 2021

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 = 15Output:Kinetic Energy = 2812.5Potential Energy = 2450Explanation : The kinetic energy of the particle is 2812.5 and the potential energy is 2450.
Input : M=5.5, H=23.5, V= 10.5Output :303.1881266.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
Java
Python3
C#
Javascript
 

 
 










// 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 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 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# 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






 

 
 










<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>






Kinetic Energy = 303.188
Potential Energy = 1266.65
 
Time Complexity: O(1)Auxiliary Space: O(1)
 
bgangwar59
ankthon
hritikrommie
parthagarwal1962000

Mathematical


School Programming

Writing code in comment? 
                                    Please use ide.geeksforgeeks.org, 
                                    generate link and share the link here.
                                
Program to find GCD or HCF of two numbers
Modulo Operator (%) in C/C++ with Examples
Merge two sorted arrays
Prime Numbers
Program to find sum of elements in a given array
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java