Calculate Distance in Nautical Miles: C Program

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.

Formula For Distance in Nautical Miles

D = 3963 * acos( sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 – lon1) );

where lat1, lat2 are latitudes. lon1, lon2 are longitude.

Formula To Convert Degree To Radian

radian = degree * (PI/180.0);
where PI is 3.141592

Logic To Find Distance In Nautical Miles

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.

Source Code: Calculate Distance in Nautical Miles: C Program

 
#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


[youtube https://www.youtube.com/watch?v=d88pWBloIOE]

YouTube Link: https://www.youtube.com/watch?v=d88pWBloIOE [Watch the Video In Full Screen.]


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

C Program To Convert Polar To Cartesian Co-ordinates

Write a C program to receive Polar co-ordinates(r, θ) and convert them into Cartesian co-ordinates(x, y). Ask the user to enter theta(θ) value in degrees.

Related Read:
C Program To Convert Cartesian To Polar Co-ordinates

Formula To Convert Polar To Cartesian Co-ordinates

x = r * cos(θ);

y = r * sin(θ);

where x and y are Cartesian co-ordinates. r and θ are polar co-ordinates.

Formula To Convert Degree To Radian

radian = degree * (PI/180.0);
where PI is 3.141592

Source Code: Convert Polar To Cartesian Co-ordinates: C Program

 
#include < stdio.h >
#include < math.h >

int main()
{
    float x, y, r, theta;
    const float PI = 3.141592;

    printf("Enter Polar Co-ordinates(r, theta)\n");
    scanf("%f%f", &r, &theta);

    /* Convert angle from Degree To Radian */    theta = theta * (PI / 180.0); 

    x = r * cos(theta);
    y = r * sin(theta);

    printf("Cartesian Co-ordinates (x, y) = (%f, %f)\n", x, y);

    return 0;
}

Output:
Enter Polar Co-ordinates(r, theta)
5.01
53.131
Cartesian Co-ordinates (x, y) = (3.005938, 4.008047)

Convert Polar To Cartesian Co-ordinates: C Program


[youtube https://www.youtube.com/watch?v=XCz3CDUsC9o]

YouTube Link: https://www.youtube.com/watch?v=XCz3CDUsC9o [Watch the Video In Full Screen.]


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

C Program To Convert Cartesian To Polar Co-ordinates

Write a C program to receive Cartesian co-ordinates(x, y) of a point and convert them into Polar co-ordinates(r, θ).

Related Read:
C Program To Convert Polar To Cartesian Co-ordinates

Formula To Convert Cartesian To Polar Co-ordinates

r = sqrt(x*x + y*y);
OR
r = sqrt( pow(x, 2) + pow(y, 2) );

theta(θ) = atan(y/x);

where x and y are Cartesian co-ordinates.
atan() is the method in math.h library for calculating tan inverse.

Formula To Convert Radian To Degree

degree = radian * (180.0 / PI);
where PI is 3.141592

Source Code: Convert Cartesian To Polar Co-ordinates: C Program

 
#include < stdio.h >
#include < math.h >

int main()
{
    float x, y, r, theta;
    const float PI = 3.141592;

    printf("Enter Cartesian Co-ordinates(x, y)\n");
    scanf("%f%f", &x, &y);

    r     = sqrt(x*x + y*y);
    theta = atan(y/x);       // Radian

    theta = theta * (180.0 / PI); //Radian To Degree Conversion

    printf("Polar Co-ordinates: (r, theta) = (%f, %f)\n", r, theta);

    return 0;
}

Output:
Enter Cartesian Co-ordinates(x, y)
3
4
Polar Co-ordinates: (r, theta) = (5.000000, 53.130112)

Convert Cartesian To Polar Co-ordinates: C Program


[youtube https://www.youtube.com/watch?v=3ebGfJ_GOD0]

YouTube Link: https://www.youtube.com/watch?v=3ebGfJ_GOD0 [Watch the Video In Full Screen.]


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

Print All Trigonometric Ratios: C Program

If value of an angle is input through the keyboard, write a c program to print all its Trigonometric Ratios.

Formula To Convert Degree To Radian

radian = degree * (PI / 180.0);
where PI = 3.14159; (constant value)

Logic to Print all Trigonometric Ratios

First we ask the user to input angle of a Triangle in degrees. Next we convert angel from degrees to radian.

Next we include math.h header file and use methods present in it to get:
sin()
cos()
tan()

To get cosec() = 1 / sin();
To get sec() = 1 / cos();
To get cot() = 1 / tan();

tan() value can also be obtained using sin() / cos();
cot() value can also be obtained using cos() / sin();

Source Code: Print All Trigonometric Ratios: C Program

 
#include < stdio.h >
#include < math.h >

int main()
{
    float degree, radian;
    const float PI = 3.14159;

    printf("Enter angle in degree\n");
    scanf("%f", °ree);

    radian = degree * (PI / 180.0);

    printf("Sin(%f) = %f\n", degree, sin(radian));
    printf("Cos(%f) = %f\n", degree, cos(radian));
    printf("Tan(%f) = %f\n", degree, tan(radian));
    printf("Cosec(%f) = %f\n", degree, 1/sin(radian));
    printf("Sec(%f) = %f\n", degree, 1/cos(radian));
    printf("Cot(%f) = %f\n", degree, 1/tan(radian));

    return 0;
}

Output 1
Enter angle in degree
30
Sin(30.000000) = 0.500000
Cos(30.000000) = 0.866026
Tan(30.000000) = 0.577350
Cosec(30.000000) = 2.000001
Sec(30.000000) = 1.154700
Cot(30.000000) = 1.732052

Output 2
Enter angle in degree
45
Sin(45.000000) = 0.707106
Cos(45.000000) = 0.707107
Tan(45.000000) = 0.999999
Cosec(45.000000) = 1.414214
Sec(45.000000) = 1.414213
Cot(45.000000) = 1.000001

Print All Trigonometric Ratios: C Program


[youtube https://www.youtube.com/watch?v=-FMdZ456FUA]

YouTube Link: https://www.youtube.com/watch?v=-FMdZ456FUA [Watch the Video In Full Screen.]


Tan and Cot

 
    printf("Tan(%f) = %f\n", degree, sin(radian)/cos(radian));
    printf("Cot(%f) = %f\n", degree, cos(radian)/sin(radian));

OR

 
    printf("Tan(%f) = %f\n", degree, tan(radian));
    printf("Cot(%f) = %f\n", degree, 1/tan(radian));

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

C Program to Generate Odd Numbers Between Two Integers

C program to generate odd numbers between 2 integer values input by the user.

Related Read:
Even or Odd Number: C Program
Even or Odd Number without using Modular Division: C Program

Note 1: An odd number is an integer that is not exactly divisible by 2.

Note 2: Odd numbers are of the form (2 * n + 1);

Note 3: Modular division( % ) returns remainder of division. For example, 10 / 2 = 5. But 10 % 2 = 0.

C Program to Generate Odd Numbers Between Two Integers

 
#include<stdio.h>

int main()
{
    int count, limit;

    printf("Enter start and end value to generate Odd numbers\n");
    scanf("%d%d", &count, &limit);

    printf("\nOdd numbers between %d and %d are:\n", count, limit);

    while(count <= limit)
    {
        if(count % 2 != 0)
        {
            printf("%d\n", count);
        }
        count++;
    }

    return 0;
}

Output 1
Enter start and end value to generate Odd numbers
10
30

Odd numbers between 10 and 30 are:
11
13
15
17
19
21
23
25
27
29

Output 2
Enter start and end value to generate Odd numbers
1
10

Odd numbers between 1 and 10 are:
1
3
5
7
9

C Program to Generate Odd Numbers Between Two Integers


[youtube https://www.youtube.com/watch?v=vPzV7pE0Djw]

YouTube Link: https://www.youtube.com/watch?v=vPzV7pE0Djw [Watch the Video In Full Screen.]


In above c program, we ask the user to input 2 integer value and store it in variables count and limit. While loop keeps executing until the start value i.e., count is less than or equal to the end value i.e., limit.

Inside while loop, for every value of count, we check if its not perfectly divisible by 2. If true, it’s a Odd number and we output that number to the console window. On every iteration of the loop we increment the value of count by 1.

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