Home > Net >  Find the value of y according to condition:
Find the value of y according to condition:

Time:10-31

Find the value of y according to condition:

enter image description here

I tried to do but something is wrong or I did not right so I need some help

#include <stdio.h>

int x, y;

int main() {
  scanf("%d", &x);
  if (x < 5)
    y = x * x - 3 * x   4;
  else
    y = x   7;
  printf("%d", y);
  return 0;
}

CodePudding user response:

Shouldn't it be:

if (x<-4) {
  y=x 5;
} else if (x>=-4 && x<=7) {
  y=x*x-3*x;
} else {
  y=x*x*x 2*x
}

CodePudding user response:

Here your piece-wise function has 3 segments in its domain. You have to divide you condition into three segments. Your syntax will be like this :

if(condition 1){
    <statement 1>
}
else if(condition 2){
    <statement 2>
}
else {
<statement 3>
}

Or you can use three 'if' statements to check the input value.

if(condition 1) {
<statement 1>
}
if(condition 2) {
<statement 2>
}
if(condition 3) {
<statement 3>
}
...
...
...

Hope you understand.

  •  Tags:  
  • c 11
  • Related