Write a C program that computes the volume of a pendulum.

Source Code

Brief explanation is provided after the source code.

#include <stdio.h>
#include <math.h>
#define PI 3.142
#define g 10

int main(int argc, char ** argv) {

    float period, l;
    printf("Enter length of pendulum: ");
    scanf("%f", &l);
    period = 2 * PI * sqrt(l/g);
    printf("Period is %.4f\n", period);

    return 0;
}

When you compile and execute the above program it produces the following result on Linux:

Enter length of pendulum: 10
Period is 6.2840

Brief Explanation

  • The program starts by requesting the user to enter the length of a pendulum.
  • Using the formula 2π(l/g)1/2, the period of the pendulum is computed.
  • Using printf function, the period is printed to the stdout.

Add comment


Security code
Refresh