Topic Collection of Zhejiang University Edition "C Language Programming (3rd Edition)"
Exercise 3-2 Calculate the value of the sign function (10 points)
For any integer n, the sign function sign(n) is defined as follows:
Please write a program to calculate the value of this function for any input integer.
Input format:
Input gives the integer n in one line.
Output format:
In one line, output the function value corresponding to the integer n in the format "sign(n) = function value".
Input example 1:
10
Output sample 1:
sign(10) = 1
Input example 2:
0
Output sample 2:
sign(0) = 0
Input example 3:
98
Sample output 3:
sign(-98) = -
1
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d",&n);
if(n<0){
printf("sign(%d) = -1",n);
}
else if(n==0){
printf("sign(%d) = 0",n);
}
else{
printf("sign(%d) = 1",n);
}
return 0;
}