Q:

C program to check positive or negative without using conditional statements

belongs to collection: C Programming on Numbers

0

In this exercise, we learn how to write a C program to check positive or negative without using conditional statements?. We will write the C program to check positive or negative without using conditional statements. It is tricky to check the positive and negative numbers without an if-else and ternary operator, we just only use the bitwise operator.

Input: 10
Output: 10 is positive
 
 
Input: -10
Output: -10 is negative
 
 
Input: 0
Output: 0 is zero

All Answers

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

The signed shift data>>(BITS -1) converts every negative number into -1 and every other into 0, where BITS is the number of bits in an integer number. Similar to that when we do a  – data>>(BITS -1), if data is a positive number then it will return -1 as we are doing – data>>(BITS -1). But both will return 0 for zero ( 0), so we can create a formula here,

(1 + (data>>(BITS -1)) -(-data>>(BITS -1)));

BITS => number of bits in an integer number.

 

Now we can check the result of the above formula for positive, negative, and zero,

1. When data is a positive integer number:

1 + 0 - (-1) => 2

2. When data is negative integer number:

1 + (-1) - 0 => 0

3. When data is zero:

1 + 0 - 0 => 1

So we know that the above-mentioned formula returns 2 when it is a positive number, returns 0 when it is a negative number, it returns 1 when it is zero.

If you do not want to use conditional statements if-else or ternary operator to display the find positive and negative number in C programming. So you need to create an array of strings that contains “negative” at the 0th index, “zero” at the 1st index, and “positive” at the 2nd index.

You need to calculate the index using the above formula and print the string according to the index. Let’s see the C program to check the positive, negative, and zero without branching statements.

#include <stdio.h>
// Total bits required to represent integer
#define BITS sizeof(int) * 8
// returns 0 if it is negative
// function to return 1 if it is zero
// returns 2 if it is positive
int numberTypeIndex(int data)
{
    return (1 + (data>>(BITS -1)) -(-data>>(BITS -1)));
}
int main()
{
    int num,numType;
    // string array to store all kinds of number
    const char* str[] = { "Negative", "Zero", "Positive" };
    printf("Enter any number: ");
    scanf("%d", &num);
    //Calling function to get index
    numType = numberTypeIndex(num);
    printf("%d is %s",num,str[numType]);
    return 0;
}

output:

Enter any number: -10
Negative number

Enter any number: 10
Positive number

Enter any number: 0
Zero

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

total answers (1)

C Programming on Numbers

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to find positive or negative using bitwi... >>
<< C Program to find Perfect Number...