C Program To Check For Alphabet, Number and Special Symbol

Any character is entered through the keyboard, write a C program to determine whether the character entered is a capital letter, a small case letter, a digit or a special symbol.

The following table shows the range of ASCII values for various characters:
Character A – Z : ASCII Value 65 – 90
Character a – z : ASCII Value 97 – 122
Character 0 – 9 : ASCII Value 48 – 57
Special Symbol : ASCII Value 0 – 47, 58 – 64, 91 – 96, 123 – 127

ascii codes

Related Read:
else if statement in C
Relational Operators In C
C Program To Print All ASCII Characters and Code

Expected Output for the Input

User Input:
Enter a Character
$

Output:
$ is a Special Character

Video Tutorial: C Program To Check For Alphabet, Number or Special Symbol


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

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

Source Code: C Program To Check For Alphabet, Number and Special Symbol

#include<stdio.h>

int main()
{
    char ch;

    printf("Enter a Character\n");
    scanf("%c", &ch);

    if(ch >= 65 && ch <= 90)
    {
        printf("%c is an Uppercase Alphabet\n", ch);
    }
    else if(ch >= 97 && ch <= 122)
    {
        printf("%c is an lowercase Alphabet\n", ch);
    }
    else if(ch >= 48 && ch <= 57)
    {
        printf("%c is a Number\n", ch);
    }
    else if( (ch >= 0  && ch <= 47) ||
             (ch >= 58 && ch <= 64) ||
             (ch >= 91 && ch <= 96) ||
             (ch >= 123 && ch <= 127))
    {
        printf("%c is a Special Character\n", ch);
    }

    return 0;
}

Output 1:
Enter a Character
A
A is an Uppercase Alphabet

Output 2:
Enter a Character
i
i is an lowercase Alphabet

Output 3:
Enter a Character
8
8 is a Number

Output 4:
Enter a Character
$
$ is a Special Character

Logic To Check For Alphabet, Number and Special Symbol

We use &&(AND) operator check check for range. i.e., for number, we check from the range 48 to 57. To check if the user entered character lies in this range we use (ch >= 48 && ch <= 57). To check for multiple ranges we use ||(OR) operator. For example, for special symbol:


(ch >= 0 && ch <= 47) ||
(ch >= 58 && ch <= 64) ||
(ch >= 91 && ch <= 96) ||
(ch >= 123 && ch <= 127)

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 In Which Quadrant The Point Lies

Lets write a C program to determine the position of point(x,y) on the graph of x and y axis.

Important: Always remember that, to specify a point, we always write x-axis value first and then the y-axis value. i.e., (x, y)

Related Read:
C Program To Check If Point Lies on x-axis or y-axis or Origin

Logic To Check The Position of the Point in the graph
We check for 9 conditions to determine the position of the user entered point in the graph.
x and y axis graph
1. Point lies on (0, 0): Origin
2. y = 0 and x > 0. i.e., x is positive: point lies on positive side of x-axis.
3. x = 0 and y > 0. i.e., y is positive: point lies on positive side of y-axis.
4. y = 0 and x < 0. i.e., x is negative: point lies on negative side of x-axis.
5. x = 0 and y < 0. i.e., y is negative: point lies on negative side of y-axis.
6. x > 0 and y > 0. i.e., both x and y are positive: point lies in First Quadrant.
7. x < 0 and y > 0. i.e., x is negative and y is positive: point lies in Second Quadrant.
8. x < 0 and y < 0. i.e., both x and y are negative: point lies in Third Quadrant.
9. x > 0 and y < 0. i.e., x is positive and y is negative: point lies in Forth Quadrant.

Expected Output for the Input

User Input:
Enter the point(x, y)
5
5

Output:
Point lies in First Quadrant

Video Tutorial: C Program To Check In Which Quadrant The Point Lies


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

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

Source Code: C Program To Check In Which Quadrant The Point Lies

#include < stdio.h >

int main()
{
    float x, y;

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

    if(x == 0 && y == 0)
    {
        printf("Point lies on the Origin\n");
    }
    else if(y == 0 && x > 0)
    {
        printf("Point lies on positive x-axis\n");
    }
    else if(x == 0 && y > 0)
    {
        printf("Point lies on positive y-axis\n");
    }
    else if(y == 0 && x < 0)
    {
        printf("Point lies on negative x-axis\n");
    }
    else if(x == 0 && y < 0)
    {
        printf("Point lies on negative y-axis\n");
    }
    else if(x > 0 && y > 0)
    {
        printf("Point lies in First Quadrant\n");
    }
    else if(x < 0 && y > 0)
    {
        printf("Point lies in Second Quadrant\n");
    }
    else if(x < 0 && y < 0)
    {
        printf("Point lies in Third Quadrant\n");
    }
    else if(x > 0 && y <0)
    {
        printf("Point lies in Forth Quadrant\n");
    }

    return 0;
}

Output 1:
Enter the point(x, y)
0
0
Point lies on the Origin

Output 2:
Enter the point(x, y)
5
0
Point lies on positive x-axis

Output 3:
Enter the point(x, y)
-5
0
Point lies on negative x-axis

Output 4:
Enter the point(x, y)
0
5
Point lies on positive y-axis

Output 5:
Enter the point(x, y)
0
-5
Point lies on negative y-axis

Output 6:
Enter the point(x, y)
5
5
Point lies in First Quadrant

Output 7:
Enter the point(x, y)
-5
5
Point lies in Second Quadrant

Output 8:
Enter the point(x, y)
-5
-5
Point lies in Third Quadrant

Output 9:
Enter the point(x, y)
5
-5
Point lies in Forth Quadrant

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 Point Lies on x-axis or y-axis or Origin

Given a point (x, y), write a C program to find out if it lies on the x-axis, y-axis or on the origin(0, 0).

Important: Always remember that, to specify a point, we always write x-axis value first and then the y-axis value. i.e., (x, y)

Logic To Check If Point(x, y) Lies on x-axis or y-axis or Origin
In point (x, y), if x = 0 and y = 0, then the point lies on the origin. If value of x is zero and y is greater than zero, then the point lies on y-axis. If y is zero and x is greater than zero, then the point lies on x-axis.

User Input:
Enter the point(x, y)
0
5

Output:
Point lies on y-axis

Video Tutorial: C Program To Check If Point Lies on x-axis or y-axis or Origin


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

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

Source Code: C Program To Check If Point Lies on x-axis or y-axis or Origin

#include < stdio.h >

int main()
{
    float x, y;

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

    if(x == 0 && y == 0)
    {
        printf("Point lies on the Origin\n");
    }
    else if(x == 0)
    {
        printf("Point lies on y-axis\n");
    }
    else if(y == 0)
    {
        printf("Point lies on x-axis\n");
    }
    else
    {
        printf("Point neither lies on x-axis nor on y-axis\n");
    }

    return 0;
}

Output 1:
Enter the point(x, y)
0
0
Point lies on the Origin

Output 2:
Enter the point(x, y)
5
0
Point lies on x-axis

Output 3:
Enter the point(x, y)
0
5
Point lies on y-axis

Output 4:
Enter the point(x, y)
5
5
Point neither lies on x-axis nor on y-axis

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 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