Experiment 3-2 Calculate the value of the sign function (10 points)

For any integer n, sign(n)the definition of the sign function is as follows:
f (x) = {− 1 n <0 0 n = 0 1 n> 0 f(x)= \begin{cases} -1& \text{n <0}\ \ 0& \text{n = 0}\\ 1& \text{n> 0} \end{cases}f(x)=101n < 0n = 0n > 0
Please write a program to calculate the value of this function for any input integer.

Input format:

The input gives an integer in one line n.

Output format:

In one line, sign(n) = 函数值output the nfunction value corresponding to the integer in the format " " .

Input example 1:

10

Output example 1:

sign(10) = 1

Input example 2:

0

Output example 2:

sign(0) = 0

Input example 3:

98

Output sample 3:

sign(-98) = -1

Code:

# include <stdio.h>
# include <stdlib.h>

int sign(int n) {
    
    
    int value = 1;
    if (n == 0) value = 0;
    else if (n < 0) value = -1;
    return value;
}
int main() {
    
    
    int n;
    scanf("%d",&n);
    printf("sign(%d) = %d",n,sign(n));
    return 0;
}

Submit screenshot:
Insert picture description here

Problem-solving ideas:

I thought it couldn't be so simple here, so I wrote a function!

Guess you like

Origin blog.csdn.net/weixin_43862765/article/details/114436193