C Program To Check If Point Lies Inside, Outside or On The Circle

Given the coordinates(cx, cy) of center of a circle and its radius, write a C program that will determine whether a point(x, y) lies inside the Circle, on the Circle or outside the Circle. (Hint: Use sqrt() and pow() functions)

Note:
Center Point – (cx, cy);
We need to find the position of point (x, y);

Logic To Check whether Point Lies Inside, Outside or On The Circle

First we need to calculate the distance of the point(x, y) from the center(cx, cy) of the circle. Next we need to compare the distance with the radius of the Circle.

Conditions To Determine The Position of the Point(x, y)
1. Distance is greater than radius: point is outside the Circle.
2. Distance is less than radius : point is inside the Circle.
3. Distance is equal to the radius: point is on the Circle.

Related Read:
Basic Arithmetic Operations In C
Calculate Power of a Number using pow(): C Program

Expected Output for the Input

User Input:
Enter the center point(cx, cy)
0
0
Enter radius of the circle
6
Enter the point(x, y) to check its position
3
3

Output:
Point (3.00, 3.00) is inside the Circle

Formula To Calculate Distance from point(x, y) To Center Point (cx, cy)

distance = sqrt( pow( (x – cx), 2 ) + pow( (y – cy), 2) );

Note: sqrt() and pow() are builtin method present in library file or header file math.h

Video Tutorial: C Program To Check If Point Lies Inside, Outside or On The Circle


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

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

Source Code: C Program To Check If Point Lies Inside, Outside or On The Circle

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

int main()
{
    float cx, cy, radius, x, y, distance;

    printf("Enter the center point(cx, cy)\n");
    scanf("%f%f", &cx, &cy);

    printf("Enter radius of the circle\n");
    scanf("%f", &radius);

    printf("Enter the point(x, y) to check its position\n");
    scanf("%f%f", &x, &y);

    distance = sqrt( pow( (x - cx), 2 ) + pow( (y - cy), 2 ) );

    if(distance < radius)
    {
        printf("Point (%0.2f, %0.2f) is inside the Circle\n", x, y);
    }
    else if(distance > radius)
    {
        printf("Point (%0.2f, %0.2f) is outside the Circle\n", x, y);
    }
    else
    {
        printf("Point (%0.2f, %0.2f) is on the Circle\n", x, y);
    }

    return 0;
}

Output 1:
Enter the center point(cx, cy)
0
0
Enter radius of the circle
6
Enter the point(x, y) to check its position
2
2
Point (2.00, 2.00) is inside the Circle

Output 2:
Enter the center point(cx, cy)
0
0
Enter radius of the circle
6
Enter the point(x, y) to check its position
12
6
Point (12.00, 6.00) is outside the Circle

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 Check If Three Points Are On One Straight Line

Given three points (x1, y1), (x2, y2) and (x3, y3), write a C program to check if all the three points fall on one straight line.

Note: (x1, y1), (x2, y2) and (x3, y3) are called co-ordinates of x and y axis.

Related Read:
Basic Arithmetic Operations In C

Expected Output for the Input

User Input:
Enter points (x1, y1)
1
2
Enter points (x2, y2)
3
4
Enter points (x3, y3)
5
6

Output:
All 3 points lie on the same straight line.

Formula To Calculate Slope of 2 points

Slope of points (x1, y1) and (x2, y2) = m;

m = (y2 – y1) / (x2 – x1);

Slope of points (x2, y2) and (x3, y3) = n;

n = (y3 – y2) / (x3 – x2);

x and y axis

Logic To Check If Three Points Are On One Straight Line

We ask the user to enter all 3 points (x1, y1), (x2, y2) and (x3, y3). Next we calculate slope of (x1, y1), (x2, y2) and (x2, y2) (x3, y3). If slopes of both these points are equal, then all these 3 points lie on same straight line.

Video Tutorial: C Program To Check If Three Points Are On Same Straight Line


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

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

Source Code: C Program To Check If Three Points Are On One Straight Line

#include < stdio.h >

int main()
{
    float x1, y1, x2, y2, x3, y3, m, n;

    printf("Enter points (x1, y1)\n");
    scanf("%f%f", &x1, &y1);

    printf("Enter points (x2, y2)\n");
    scanf("%f%f", &x2, &y2);

    printf("Enter points (x3, y3)\n");
    scanf("%f%f", &x3, &y3);

    m = (y2 - y1) / (x2 - x1);
    n = (y3 - y2) / (x3 - x2);

    if( m == n)
    {
        printf("All 3 points lie on the same line\n");
    }
    else
    {
        printf("All 3 points do not lie on the same line\n");
    }

    return 0;
}

Output 1:
Enter points (x1, y1)
-2
2
Enter points (x2, y2)
2
5
Enter points (x3, y3)
6
8
All 3 points lie on the same straight line.

Output 2:
Enter points (x1, y1)
1
2
Enter points (x2, y2)
3
4
Enter points (x3, y3)
-10
15
All 3 points do not lie on the same straight line.

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 Find Whether Area of Rectangle Is Greater Than Its Perimeter

Given the length and breadth(or width) of a rectangle, write a C program to find whether the area of the rectangle is greater than its perimeter. For example, the area of the rectangle with length = 5 and breadth = 4 is greater than its perimeter.

Related Read:
Area of Rectangle: C Program
Perimeter of Rectangle: C Program

Note:
Rectangle is a quadrilateral(i.e., it has 4 sides). And all the 4 angles of the rectangle are of 90 degree. To calculate area of a rectangle we need its length and width/breadth.

Formula To Calculate Area of a Rectangle

Area = Length * Width;
area of rectangle

Formula To Calculate Perimeter of a Rectangle

Perimeter = 2 * (Length + Width);
perimeter of rectangle

Length: is the length of the longest side of the rectangle.
width: is the length of the smallest side of the rectangle.

Expected Output for the Input

User Input:
Enter length of the Rectangle
5
Enter width of the Rectangle
4

Output:
yes, area(20.00) is greater than its perimeter(18.00)

Video Tutorial: C Program To Find Whether Area of Rectangle Is Greater Than Its Perimeter


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

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

Source Code: C Program To Find Whether Area of Rectangle Is Greater Than Its Perimeter

#include < stdio.h >

int main()
{
    float length, width, area, perimeter;

    printf("Enter length of the Rectangle\n");
    scanf("%f", &length);

    printf("Enter width of the Rectangle\n");
    scanf("%f", &width);

    area      =     (length * width);
    perimeter = 2 * (length + width);

    if(area > perimeter)
    {
        printf("yes, area(%0.2f) is greater than its perimeter(%0.2f)\n", 
               area, perimeter);
    }
    else
    {
        printf("Area(%0.2f) is not greater than its perimeter(%0.2f)\n", 
               area, perimeter);
    }

    return 0;
}

Output:
Enter length of the Rectangle
12
Enter width of the Rectangle
6
yes, area(72.00) is greater than its perimeter(36.00)

Logic To Find Whether Area of Rectangle Is Greater Than Its Perimeter

We ask the user to enter length and breadth/width of the rectangle and store it inside address of variable length and width. Next we calculate area and perimeter of the rectangle using the user entered value for length and width of the rectangle.

Area = Length * Width;
Perimeter = 2 * (Length + Width);

Next we check if the value present in variable area is greater than the value of perimeter or not, and output the result accordingly to the console window.

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 Determine Youngest of Three

If the ages of Ram, Shyam and Ajay are input through the keyboard, write a C program to determine the youngest of the three.

Related Read:
Decision Control Instruction In C: IF
Relational Operators In C

Expected Output for the Input

User Input:
Enter the age of Ram, Shyam and Ajay
20
20
30

Output:
Ram is the youngest
Shyam is the youngest

Video Tutorial: C Program To Determine Youngest of Three


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

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

Source Code: C Program To Determine Youngest of Three

#include<stdio.h>

int main()
{
    int ram, shyam, ajay;

    printf("Enter the age of Ram, Shyam and Ajay\n");
    scanf("%d%d%d", &ram, ­­­­&shyam, &ajay);

    if(ram <= shyam && ram <= ajay)
    {
        printf("Ram is the youngest\n");
    }

    if(shyam <= ram && shyam <= ajay)
    {
        printf("Shyam is the youngest\n");
    }

    if(ajay <= ram && ajay <= shyam)
    {
        printf("Ajay is the youngest\n");
    }

    return 0;
}

Output 1:
Enter the age of Ram, Shyam and Ajay
20
30
20
Ram is the youngest
Ajay is the youngest

Output 2:
Enter the age of Ram, Shyam and Ajay
20
30
19
Ajay is the youngest

Logic To Determine Youngest of Three

In this C program we ask the user to enter age for 3 people – Ram, Shyam and Ajay. We store it inside the address of variables ram, shyam and ajay.

We check if Ram’s age is less than or equal to Shyam and Ajay’s age. If true Ram is the youngest person.

We check if Shyam’s age is less than or equal to Ram and Ajay’s age. If true Shyam is the youngest person.

We check if Ajay’s age is less than or equal to Ram and Shyam’s age. If true Ajay is the youngest person.

We write separate if condition for each person. If we take else if clause it gives wrong result when 2 or more people have same age.

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 Calculate Profit or Loss

If cost price and selling price of an item are input through the keyboard, write a C program to determine whether the seller has made profit or incurred loss. Also determine how much profit he made or loss he incurred.

Related Read:
else if statement in C
Relational Operators In C

Note:
Cost Price: is the price paid by the seller for the product or the manufacturing cost of the product.

Selling Price: is the price seller sold the product for.

Formula To Calculate Profit

profit = (selling_price – cost_price);

Formula To Calculate Loss

loss = (cost_price – selling_price);

Expected Output for the Input

User Input:
Enter the cost price of the product
1500
Enter the selling price of the product
2000

Output:
Your profit is 500

Video Tutorial: C Program To Calculate Profit or Loss


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

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

Source Code: C Program To Calculate Profit or Loss

#include < stdio.h >

int main()
{
    int cp, sp;

    printf("Enter the cost price of the product\n");
    scanf("%d", &cp);

    printf("Enter the selling price of the product\n");
    scanf("%d", &sp);

    if(sp > cp)
    {
        printf("Your profit is %d\n", (sp-cp));
    }
    else if(cp > sp)
    {
        printf("Loss Incurred is %d\n", (cp-sp));
    }
    else
    {
        printf("Neither profit, nor loss\n");
    }

    return 0;
}

Output 1:
Enter the cost price of the product
2000
Enter the selling price of the product
2500
Your profit is 500

Output 2:
Enter the cost price of the product
2000
Enter the selling price of the product
1500
Loss Incurred is 500

Output 3:
Enter the cost price of the product
1500
Enter the selling price of the product
1500
Neither profit, nor loss

Logic To Calculate Profit or Loss

We ask the user to enter cost price as well as selling price. If selling price is more than cost price, then the seller is in profit. If the cost price is more than selling price, then the seller incurred loss.

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