Zhejiang University Edition "C Language Programming (3rd Edition)" Exercise 3-2

Exercise 3-2 Calculate the value of a symbolic function (10 points)

For any integer n, the definition of the sign function sign (n) is as follows:
Insert picture description here
Please write a program to calculate the value of this function for any input integer.

Input format:
Enter the integer n given on one line .

Output format:
The function value corresponding to the integer n is output in a line according to the format "sign (n) = function value".

Input example 1:

10

Sample output 1:

sign(10) = 1

Input example 2:

0

Sample output 2:

sign(0) = 0

Input example 3:

-98

Sample output 3:

sign(-98) = -1

Let me go directly to the code , so simple, you will all have:

#include"stdio.h"
int main()
{
    int n, sign;
    scanf("%d", &n);
    if(n < 0)
    {
        sign = -1;
    }
    else if(n == 0)
    {
        sign = 0;
    }
    else if(n > 0)
    {
        sign = 1;
    }
    printf("sign(%d) = %d", n, sign);
    return 0;
}
Published 25 original articles · won 3 · views 240

Guess you like

Origin blog.csdn.net/oxygen_ls/article/details/105442731