Skip to content

luc006.c

Problem Statement

Write a 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. The formula for distance in nautical miles is : D = 3963 cos^-1(sin L1 sin L2 + cos L1 cos L2 * cos(G2 - G1))

Metadata

Property Detail
Author Amit Dutta (amitdutta4255@gmail.com)
License MIT
Difficulty Beginner (index: 1 / 10)

Concepts

Beta Feature

This concept detection system is still in beta and may occasionally show incorrect or incomplete results.

  • Pointers

Actions

Raw View on GitHub

You can print or save this file by opening Raw and using your browser.

Source Code

#include <stdio.h>
#include <math.h>
int main()
{
    double l1, l2, g1, g2, d;
    printf("Enter the Latitude in 'L1, L2' format : ");
    scanf("%lf, %lf", &l1, &l2);
    printf("Enter the Longitude in 'G1, G2' format : ");
    scanf("%lf, %lf", &g1, &g2);
    // Converting degree to radian because function from math.h use radian not degree
    l1 = l1 * (M_PI / 180);
    l2 = l2 * (M_PI / 180);
    g1 = g1 * (M_PI / 180);
    g2 = g2 * (M_PI / 180);
    d = 3963 * acos(sin(l1) * sin(l2) + cos(l1) * cos(l2) * cos(g2 - g1));
    printf("Distance in nautical miles : %g", d);
    return 0;
}

Explanation

Explain with AI

Copy the prompt below and paste it into any AI assistant.

    You are explaining a C programming code to a beginner.

    STRICT RULES:

    - Only use the given code. Do NOT assume anything not present.

    - Do NOT add extra examples.

    - Keep explanation clear and short.

    - If something is unclear, say "Not clear from code".

    - Follow the exact format below. Do NOT change headings.

    FORMAT:

    [START]

    ## What it does

    (Explain the overall purpose in 1-2 sentences)

    ## Step-by-step

    (Explain how the code works in steps, simple language)

    ## Key Concepts

    (List concepts like loop, condition, function, etc.)

    ## Notes

    (Mention any limitations, errors, or assumptions)

    [END]

    CODE (luc006.c):

    #include <stdio.h>
    #include <math.h>
    int main()
    {
        double l1, l2, g1, g2, d;
        printf("Enter the Latitude in 'L1, L2' format : ");
        scanf("%lf, %lf", &l1, &l2);
        printf("Enter the Longitude in 'G1, G2' format : ");
        scanf("%lf, %lf", &g1, &g2);
        // Converting degree to radian because function from math.h use radian not degree
        l1 = l1 * (M_PI / 180);
        l2 = l2 * (M_PI / 180);
        g1 = g1 * (M_PI / 180);
        g2 = g2 * (M_PI / 180);
        d = 3963 * acos(sin(l1) * sin(l2) + cos(l1) * cos(l2) * cos(g2 - g1));
        printf("Distance in nautical miles : %g", d);
        return 0;
    }