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