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

Draw Rectangle: HTML5 Canvas

This video tutorial illustrates the drawing of rectangle with styling.

Tutorial Includes:
Drawing rectangle.
Specifying x-axis and y-axis for drawing rectangle.
Specifying width and height for drawing rectangle.
Filling the border(strokeStyle) with color.
Filling the body(fillStyle) of rectangle with color.
Specifying the border width(lineWidth). etc

HTML5-canvas-rectangle-xaxis-yaxis


HTML file
index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
< !DOCTYPE HTML>
<html>
<head>
<title>Canvas: HTML5</title>
<meta charset="utf-8"/>
<link href="myStyle.css" rel="stylesheet" />
<script src="myScript.js"></script>
</head>
<body>
 <canvas id="myCanvas" width="200" height="200">
  Upgrade Browser
 </canvas>
</body>
</html>

Here we’ve attached a stylesheet and a script file.
It also has a canvas element with an id of myCanvas, with 200px width and height.

CSS file
myStyle.css

1
2
3
canvas {
border: 2px dotted black;
}

We are assigning a 2px wide black dotted border to the canvas element.

JavaScript file
myScript.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
window.onload = canvas;
 
function canvas()
{
var myCanvas = document.getElementById("myCanvas");
 
if( myCanvas && myCanvas.getContext("2d") ) 
{
var context = myCanvas.getContext("2d");
context.lineWidth   = 2;
context.strokeStyle = "red";
context.strokeRect(10, 10, 100, 100);
 
context.fillStyle = "blue";
context.fillRect(10, 10, 100, 100);
}
}

Related Read: Canvas Basics: HTML5

Once the index.html file loads, it calls a method called canvas.

Validation
Inside canvas method, we check if the canvas element is actually present on the webpage. If present, we check if the browser actually supports context object.

If both of them are present, then we make use of the API provided by context object and draw the rectangle and assign some styling to it.

Defaults
By default, lineWidth has 1px. In our example we’re setting it to 2px.
By default, strokeStyle and fillStyle has black color. In our example, we’re setting it to red and blue respectively.

Result
Canvas element has some margin by default. And the top left corner of canvas is considered x-axis = 0 and y-axis = 0.
In our example, we are placing our rectangle at 10px x-axis and 10px y-axis, with 100px width and height.

With all these, we get a 100px wide and 100px tall, blue rectangle with a red border.

Draw Rectangle: HTML5 Canvas


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

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



Now you maybe thinking, we’ll have fillCircle and strokeCircle, fillTriangle and strokeTriangle methods to draw circles and triangles. But NO. We don’t have such methods.
We need to achieve shapes other than rectangle using paths and lines. Lets see how it can be done in our coming video tutorials. Stay subscribed.