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 Find First and Last Digit of a Number

Write a C program to find first and last digit of the user input number, without using looping.

Related Read:
Basic Arithmetic Operations In C

Source Code: C Program to Find First and Last Digit of a Number

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

int main()
{
    int num, first, last, count;

    printf("Enter an integer number\n");
    scanf("%d", &num);

    count = log10(num);

    first = num / pow(10, count);
    last  = num % 10;

    printf("First Digit = %d\nLast Digit = %d\n", first, last);

    return 0;
}

Output 1:
Enter an integer number
123
First Digit = 1
Last Digit = 3

Output 2:
Enter an integer number
123456
First Digit = 1
Last Digit = 6

Output 3:
Enter an integer number
15937
First Digit = 1
Last Digit = 7

Output 4:
Enter an integer number
5986
First Digit = 5
Last Digit = 6

Output 5:
Enter an integer number
964801
First Digit = 9
Last Digit = 1

C Program to Find First and Last Digit of a Number


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

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


Logic To Find First and Last Digits of a Number

If user enters a number 123. i.e., num = 123; Then num % 10 would give the last digit of the number i.e., 3. To get first digit, we need to know the number of digits present in the number. To get that we make use of a built-in method called log10(). log10() returns the number of digits present in the number minus 1. That is because, it starts the count from 0. So if num = 123, log10(num) will return 2 and not 3. We store the number of digits inside variable count.

Now we use pow() method and calculate 10 to the power of count(value present in variable count) i.e., pow(10, count); We divide the user entered number by pow(10, count); to get the first digit of the number.
i.e., First_Digit = num / pow(10, count);

Note: Both pow() and log10() are built-in methods present in header file math.h So we include that library file at the beginning of our C program source code.

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

C Program to print Armstrong Numbers Between Two Integers

An Armstrong number or Narcissistic number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number itself.

Example 1:
If number = 153
It has 3 digits: 1, 5 and 3. So n = 3.
result = 13 + 53 + 33 = 1 + 125 + 27 = 153.
So the original number 153 is equal to the result. So it’s an Armstrong Number.

Example 2:
If number = 1634
It has 4 digits: 1, 6, 3 and 4. So n = 4.
result = 14 + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1634.
So the original number 1634 is equal to the result. So it’s an Armstrong Number.

Related Read:
C Program To Reverse a Number
Basic Arithmetic Operations In C
while loop in C programming
if else statement in C
Calculate Sum of Digits: C Program
C Program to Check Armstrong Number
C Program to print Armstrong Numbers between 1 and 500

C Program to print Armstrong Numbers Between Two User Entered Integers


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

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


To count the digits in a number

        num = temp = count;
        n   = sum  = 0;

        while(temp)
        {
            temp = temp / 10;
            n++;
        }

To Iterate through the digits in the number

        while(num)
        {
            rem = num % 10;
            sum = sum + pow(rem, n);
            num = num / 10;
        }

To know above code logic, please visit and watch the video tutorial present at C Program to Check Armstrong Number.

Full Source Code: C Program to print Armstrong Numbers Between Two User Entered Integers

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

int main()
{
    int count, limit, rem, temp, num, n;
    float sum;

    printf("Enter 2 integer numbers\n");
    scanf("%d%d", &count, &limit);

    printf("\nArmstrong numbers between %d and %d\n", count, limit);

    while(count <= limit)
    {
        num = temp = count;
        n   = sum  = 0;

        while(temp)
        {
            temp = temp / 10;
            n++;
        }

        while(num)
        {
            rem = num % 10;
            sum = sum + pow(rem, n);
            num = num / 10;
        }

        if(count == sum)
        {
            printf("%d is Armstrong number\n", count);
        }

        count++;
    }

    return 0;
}

Output:
Enter 2 integer numbers
1
99999

Armstrong numbers between 1 and 99999
1 is Armstrong number
2 is Armstrong number
3 is Armstrong number
4 is Armstrong number
5 is Armstrong number
6 is Armstrong number
7 is Armstrong number
8 is Armstrong number
9 is Armstrong number
153 is Armstrong number
370 is Armstrong number
371 is Armstrong number
407 is Armstrong number
1634 is Armstrong number
8208 is Armstrong number
9474 is Armstrong number
54748 is Armstrong number
92727 is Armstrong number
93084 is Armstrong number

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

Calculate Power of a Number using pow(): C Program

Two numbers are entered through the keyboard. Write a program to find the value of one number raised to the power of another by using pow() method present in math.h library.

Related Read:
while loop in C programming
Calculate Power of a Number: C Program

One Number Raised To Another using pow(): C Program


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

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


Source Code:One Number Raised To Another using pow(): C Program

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

int main()
{
    int base, exponent;

    printf("Enter base value\n");
    scanf("%d", &base);

    printf("Enter exponent value\n");
    scanf("%d", &exponent);

    printf("%d to the power of %d is %2f\n",
           base, exponent, pow(base, exponent));

    return 0;
}

Output:
Enter base value
25
Enter exponent value
3
25 to the power of 3 is 15625.000000

Note:
pow() method is present in math.h library, so we include it at the top of our c source code.

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