This program asks the user to enter any number. Next, this c program checks whether the given value is positive or negative or zero using the if-else statement.
#include <stdio.h> // Total bits required to represent integer #define BITS sizeof(int) * 8 //Logic to check whether a number is positive int isPositive(int n) { return (!( n & (1 << (BITS -1 )) | (!n))); } int main() { int num,numType; printf("Enter any number: "); scanf("%d", &num); numType = isPositive(num); if(numType && num) { printf("Positive number\n"); } else if (num) { printf("Negative number\n"); } else { printf("Zero\n"); } return 0; }
Output:
Enter any number: -10Negative number
Enter any number: 10Positive number
Enter any number: 0Zero
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Output:
Enter any number: -10
Negative number
Enter any number: 10
Positive number
Enter any number: 0
need an explanation for this answer? contact us directly to get an explanation for this answerZero