Q:

Compute the sign of an integer?

0

Compute the sign of an integer?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

The MSB bit of a number defines their sign. If the MSB bit is set, the number will be negative.

#include <stdio.h>
int main()
{
    int sign = 0;
    int data = 0;
    printf("Enter the number\n");
    scanf("%d",&data); //Get the number
    sign = (data > 0) - (data < 0); // check the sign of the number
    if(sign == 1)
    {
        printf("Enter number is a positve number\n");
    }
    else if(sign == -1)
    {
        printf("Enter number is a negative number\n");
    }
    else
    {
        printf("Enter number is zero\n");
    }
    return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Detect if two integers have opposite signs?... >>