14. Write a program in C to check if a number is negative without using the 'if' statement.
In this tutorial post, we will learn how to write a C program to test whether a value is positive or negative without using conditional statements. We will write a C program to test whether a value is positive or negative without using conditional statements. It's hard to test positive and negative numbers without if-else statements, we only use bitwise operators.
Input: 25
Output: 25 is positive
Input: -100
Output: -100 is negative
Input: 0
Output: 0 is zero
Signed Shift Data >>(BITS-1) Converts every negative number to -1 and all others to 0, where bits is the number of bits in the whole number. As we do - data>>(BITS-1), if the data is a positive number it will return -1, as we do - data>>(BITS-1).
But both will return 0 for zero value (0), so we can build a formula here:
(1 + (data>>(BITS-1)) -(-data>>(BITS-1)));
BITS=> Number of bits in a whole integer number.
Let us study and check the result of the above formula for positive, negative, and zero,
1. When data is negative integer number:
1 + (-1) - 0 => 0
2. When data is a positive integer number:
1 + 0 - (-1) => 2
3. When data is zero 0:
1 + 0 - 0 => 1
So we know that the above formula returns 2 for a positive number, 0 for a negative number, 1 for zero.
If you don't want to use conditional if-else statement or ternary operator ? to find positive and negative numbers in C programming. So, you need to create an array of strings with "negative" at the 0th index, "zero" at the first index and "positive " in the second index.
You want to calculate the index using the above formula and print the row as per the index.
Let's look at a C program that checks positive, negative and zero without if else branch statement.
#include <stdio.h>
// Total numbers of bits required to represent integer
#define BITS sizeof(int) * 8
// 0 if it is negative
// 1 if it is zero
// returns 2 if it is positive
int main()
{
int n,numType;
// str array is string array that stores all kinds of number
const char* str[] = { "Negative", "Zero", "Positive" };
printf("Enter any number: ");
scanf("%d", &n);
//Calling function to get index
numType = (1 + (n>>(BITS -1)) -(-n>>(BITS -1)));
printf("%d is %s",n,str[numType]);
return 0;
}
Output:
Enter any number: -10
Negative number
Enter any number: 10
Positive number
Enter any number: 0
Zero
1.Write a program to add two numbers using the '+' operator.
2.Write a program in C to subtract two numbers using the '-' operator.
3.Write a program in C to multiply two numbers using the '*' operator.
4.Write a program to divide two numbers using the '/' operator.
5.Write a program in C to find the remainder of two numbers using the '%' operator.
6.Implement a program in C to perform bitwise AND on two numbers using the '&' operator.
7.Write a program in C to perform bitwise OR on two numbers using the '|' operator.
8.Write a program to perform bitwise XOR on two numbers using the '^' operator.
9.Write a program in C to perform bitwise left shift on a number using the '<<' operator.
10.Implement a program in C to perform bitwise right shift on a number using the '>>' operator.
11.Write a program to in C check if a number is even or odd using the bitwise AND operator.
12.Write a program in C to swap two numbers without using a temporary variable.
13.Write a program in C to find the minimum of two numbers without using the 'if' statement.
15. Write a program in C to toggle a particular bit in a number using bitwise XOR.
Other Topic:-->>Nested While Loop. || conditional statements Assignments in C.