-1

I have just started learning c programming.

For the question, I wrote the following code. Can you please help me find the error? I am not getting the desired result and the statement in the last else gets executed always.

#include<stdio.h>
#include<conio.h>

void dummy(float *a) 
{
  float b=*a; //perform some floating access
  dummy (&b); //calling a floating point function
}

void main()
{
  double x,y;

  clrscr();

  scanf("%lf %lf",x,y);

  if(x==0 && y!=0)
  { 
    printf("The point lies on the y-axis.");
  }
  else if(y==0 && x!=0 )
  { 
    printf("The point lies on the x-axis.");
  }
  else if(x==0 && y==0)
  { 
    printf("The point is the origin");
  }
  else
  {
    printf("The point lies neither on the x nor the y axis ");
  }
  getch();
}
Martin Verjans
  • 4,675
  • 1
  • 21
  • 48
joey lang
  • 187
  • 2
  • 8

4 Answers4

2

while reading values from keyboard with scanf you need to add & infront of variable.

instead

scanf("%lf %lf",x,y);

Use

scanf("%lf %lf",&x,&y);

Update

you dont have to check every time for both y and x

instead if(x==0 && y!=0) use only one, if(x==0) or if(y==0) Try:

void main()
{
   double x,y;
   clrscr();

   scanf("%lf %lf",&x,&y);

   if(x==0 && y==0)
   { 
       printf("points lies on origin.");
   }
   else if(y==0)
   { 
       printf("points lies on y-axis.");
   }
   else if(x==0)
   { 
       printf("points lies on x-axis");
   }
   else
   {
       printf("The point lies neither on the x nor the y axis ");
   }
   getch();
}
  • 2
    check my answer now, you need to check only `x==0` or `y==0` at a time –  Aug 11 '17 at 11:41
1

For to check if is equal use macro or function like

#define FEQUAL(x,y,err)            (fabs((x) - (y)) < (err))
0___________
  • 60,014
  • 4
  • 34
  • 74
1

Undefined reference to clrscr() function. So, you can try removing clrscr() function from your code.

#include<stdio.h>
#include<conio.h>

void dummy(float *a)
{
  float b=*a; //perform some floating access
  dummy (&b); //calling a floating point function
}

void main()
{
  double x,y;
  scanf("%lf %lf",&x,&y);

  if(x==0 && y!=0)
 {
    printf("The point lies on the y-axis.");
 }
  else if(y==0 && x!=0 )
 {
    printf("The point lies on the x-axis.");
 }
  else if(x==0 && y==0)
 {
    printf("The point is the origin");
 }
  else
 {
    printf("The point lies neither on the x nor the y axis ");
 }
    getch();
 }

It will work.

Jagadish
  • 45
  • 6
1

A simple solution may be as

#include <stdio.h>

int main()
{

    int x,y;
    printf("Enter the point ");
    scanf("%d,%d",&x,&y);
    if (y==0)
    {
        if (x==0)
        {
            printf("\nPonit lies on the origin\n");
        }
        else
            printf("\nPoint lies on X-axis\n");
    }
    else if (x==0)
    {
        if (y==0)
        {
            printf("\nPoint lies on the origin\n");
        }
        else
            printf("\nPoint lies on the Y-axis\n");
    }
    else
        printf("\nPoint lies in X-Y coordinate\n");
    return 0;
}
Piotr Labunski
  • 1,638
  • 4
  • 19
  • 26
Ashfi Rahman
  • 45
  • 1
  • 3
  • 7