C language that will read the value of x

WAP in C language that will read the value of x and evaluate the following function
1 for x < 0
y = { 0 for x = 0
−1 for x > 0
Using (a) nested if statements (b) using else if statements © using conditional operator

Looks like homework.

1 Like
#include

int main()
{
  int x, y;
  scanf("%d",&x);
  
  /* Using nested if statement
  if(x)
  {
     if(x < 0)
       y = 1;
     if(x > 0)
       y = -1; 
     if(x == 0)
       y = 0;   
  }
  printf("%d",y);
  */
  
  /* Using else if 
  if(x < 0)
    y = 1;
  else if(x > 0)
    y = -1;
  else if(x == 0)
    y = 0;
    
  printf("%d",y);  
  */
  
  
  /* using conditional operator
  if(x == 0)
    y = 0;
  else
    y = x > 0 ? -1 : 1;
    
  printf("%d",y);    
  */
  
  return 0;
}