Write a C program to receive values of latitude(L1, L2) and longitude(G1, G2), in degrees, of two places on the earth and output the distance (D) between them in nautical miles.
Page Contents
D = 3963 * acos( sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 – lon1) );
where lat1, lat2 are latitudes. lon1, lon2 are longitude.
radian = degree * (PI/180.0);
where PI is 3.141592
User input the values of latitude(lat1, lat2) and longitude(lon1, lon2 ), in degrees. We convert it to radians. Next we use the formula below:
D = 3963 * acos( sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 – lon1) );
and out put the result on to the console.
#include < stdio.h > #include < math.h > int main() { float lat1, lat2, lon1, lon2, D; const float PI = 3.141592; printf("Enter latitude(L1, L2)\n"); scanf("%f%f", &lat1, &lat2); printf("Enter longitude(G1, G2)\n"); scanf("%f%f", &lon1, &lon2); /* Convert Degrees To Radian */ lat1 = lat1 * ( PI / 180.0 ); lat2 = lat2 * ( PI / 180.0 ); lon1 = lon1 * ( PI / 180.0 ); lon2 = lon2 * ( PI / 180.0 ); D = 3963 * acos( sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1) ); printf("Distance in nautical miles is %f\n", D); return 0; }
Output:
Enter latitude(L1, L2)
52.3
43.2
Enter longitude(G1, G2)
12.3
15.6
Distance in nautical miles is 647.682434
Calculate Distance in Nautical Miles: C Program
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert