Program to calculate Kinetic Energy and Potential Energy
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
#include <bits/stdc++.h>
using namespace std;
float kineticEnergy( float M, float V)
{
float KineticEnergy;
KineticEnergy = 0.5 * M * V * V;
return KineticEnergy;
}
float potentialEnergy( float M, float H)
{
float PotentialEnergy;
PotentialEnergy = M * 9.8 * H;
return PotentialEnergy;
}
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
class GFG{
static double kineticEnergy( double M, double V)
{
double KineticEnergy;
KineticEnergy = 0.5 * M * V * V;
return KineticEnergy;
}
static double potentialEnergy( double M, double H)
{
double PotentialEnergy;
PotentialEnergy = M * 9.8 * H;
return PotentialEnergy;
}
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));
}
}
|
Python3
def kineticEnergy(M, V):
KineticEnergy = 0.5 * M * V * V
return KineticEnergy
def potentialEnergy(M, H):
PotentialEnergy = M * 9.8 * H
return PotentialEnergy
if __name__ = = "__main__" :
M = 5.5
H = 23.5
V = 10.5
print ( "Kinetic Energy = " , kineticEnergy(M, V))
print ( "Potential Energy = " , potentialEnergy(M, H))
|
C#
using System;
using System.Collections.Generic;
class GFG{
/// Function to calculate Kinetic Energy
static double kineticEnergy( double M, double V)
{
double KineticEnergy;
KineticEnergy = 0.5 * M * V * V;
return KineticEnergy;
}
static double potentialEnergy( double M, double H)
{
double PotentialEnergy;
PotentialEnergy = M * 9.8 * H;
return PotentialEnergy;
}
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));
}
}
|
Javascript
<script>
function kineticEnergy(M, V)
{
let KineticEnergy;
KineticEnergy = 0.5 * M * V * V;
return KineticEnergy;
}
function potentialEnergy(M, H) {
let PotentialEnergy;
PotentialEnergy = M * 9.8 * H;
return PotentialEnergy;
}
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))
</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
Share your thoughts in the comments
Please Login to comment...