Q:
C program to check positive or negative without using conditional statements
belongs to collection: C Programming on Numbers
C Programming on Numbers
- C Program to Print Even Numbers from 1 to N
- C Program to Print Even Numbers from 1 to N without If Statement
- C Program to Print Even Numbers in a Given Range
- C Program to Print Odd Numbers from 1 to N
- C Program to Print Odd Numbers in a Given Range
- How to find whether a given number is prime number in C?
- Optimize way to find an nth Fibonacci number using c programming
- C program to find a neon number
- write a program to check and print neon numbers in a given range
- C program to print natural numbers from 1 to n
- C program to print natural numbers within a range
- C Program to find the sum of natural numbers upto n
- C Program to find the sum of natural numbers within a range
- C Program to find the sum of odd natural numbers from 1 to n
- C Program to find the sum of odd numbers within a range
- C Program to find given number is the sum of first n natural numbers using Binary Search
- C Program to swap two numbers using a third variable or temp variable
- C Program to find given number is sum of first n natural numbers
- C Program to swap two numbers using arithmetic operator:
- C Program to find Perfect Number
- C program to check positive or negative without using conditional statements
- C program to find positive or negative using bitwise operators and if-else
- C program to find the negative or positive number using bitwise operators and ternary operators
- C program to count number of digits in a number
- C program to reverse digits of an integer with overflow handled
- C Program to find reverse of a number using a function
- C Program to reverse digits of a number
- C Program to swap two numbers using ex-or operator:
- C Program to find given number is the sum of first n natural numbers using Binary Search
- C program to find the generic root of a number
- C Program to calculate the square of a number using a function
- C program to find square of a number
- C Program to print the two digit number in words
- C program to find all roots of a quadratic equation using switch case
- C program to find the roots of a quadratic equation using a function
- C Program to Find the Roots of a Quadratic Equation using if-else
- Find Perfect Number using the function
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,
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:
2. When data is negative integer number:
3. When data is zero:
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.
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