# C Program to Calculate Potential Energy

Hello coders, today we will learn how to calculate the potential energy of an object using C Programming language.

Potential energy is the energy held by an object because of its position relative to other objects, stresses within itself, its electric charge or other factors.

The formula for potential energy depends on the force acting on the two objects.

Potential Energy or (P.E.) = m x g x  h where,

• m is the mass in kilograms.
• g is the acceleration due to gravity
• h is the height in meters.

This program uses the above formula to calculate potential energy of an object.

So, without further ado, let’s begin this tutorial.

Contents

## C Program to Calculate Potential Energy

```// C Program to Calculate Potential Energy
#include <stdio.h>
int main(){
float m, h, g = 9.8, PE;

printf("Enter the mass: ");
scanf("%f", &m);
printf("Enter the height: ");
scanf("%f", &h);

// Calculating potential energy
PE  = m * g * h;

// Displaying output
printf("Potential Energy is: %.2f", PE);

return 0;
}
```

Output

``````Enter the mass: 5
Enter the height: 18
Potential Energy is: 882.00
``````

## How Does This Program Work ?

```    float m, h, g = 9.8, PE;
```

In this program, we have declared four float data type variables named m, h, g and PE.

```    // Asking for input
printf("Enter the mass: ");
scanf("%f", &m);
printf("Enter the height: ");
scanf("%f", &h);
```

Now, the user is asked to enter the mass and height of the object. These values get stored in the m and h named variable.

```    // Calculating potential energy
PE  = m * g * h;
```

We calculate the potential energy of the object using the formula:

Potential Energy = mass x gravity x height

```    // Displaying output
printf("Potential Energy is: %.2f", PE);
```

The equivalent potential energy is displayed on the screen using printf() function.

## C Program to Calculate Potential Energy Using Functions

```// C Program to Calculate Potential Energy Using Functions
#include <stdio.h>

float potential_energy(float m, float h){
float energy;

// Calculating energy
energy = m * 9.8 * h;

return energy;
}

int main(){
float mass, height, PE;

printf("Enter the mass of the object: ");
scanf("%f", &mass);
printf("Enter the height of the object: ");
scanf("%f", &height);

// Calling out user-defined function
PE = potential_energy(mass, height);

// Displaying output
printf("Potential Energy is: %.2f", PE);
return 0;
}
```

Output

``````Enter the mass of the object: 15
Enter the height of the object: 40
Potential Energy is: 5880.00
``````

## Conclusion

I hope after going through this post, you understand how to calculate potential energy using the C Programming language.

If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to assist you.