Posted on by Kalkicode
Code Bit Logic

# Check whether a number is positive or negative or zero

The given problem is to create a program that checks whether a given number is positive, negative, or zero. The program takes an integer as input and determines its sign. It then outputs the corresponding result as "Positive," "Negative," or "Zero."

## Problem Statement

The task is to develop a C program that takes an integer as input, checks its sign, and prints the appropriate message indicating whether the number is positive, negative, or zero.

## Example

Let's consider an example to illustrate the problem. Suppose we have the following input numbers: 6, -6, 0, and 12.

• For the input number 6, the program should output "Positive."
• For the input number -6, the program should output "Negative."
• For the input number 0, the program should output "Zero."
• For the input number 12, the program should output "Positive."

## Standard Pseudocode

The following is the standard pseudocode for the given problem:

``````function numberSign(num):
Define output array ["Negative", "Zero", "Positive"]
status = ((num >> 31) - (-num >> 31)) + 1
Print "Number:", num
Print "Result:", output[status]

function main():
Call numberSign(6)
Call numberSign(-6)
Call numberSign(0)
Call numberSign(12)
``````

## Algorithm and Explanation:

1. Start with the `main` function.
2. Call the `numberSign` function with different test cases: 6, -6, 0, and 12.
3. Inside the `numberSign` function, an array `output` is defined with three elements: "Negative," "Zero," and "Positive." These elements correspond to the three possible outcomes.
4. The variable `status` is calculated using bit manipulation. The expression `((num >> 31) - (-num >> 31)) + 1` computes the sign of the input number efficiently.
5. The input number and the corresponding result are printed using `printf` statements.
6. The program then returns to the `main` function, where the next test case is processed.

## Code Solution

Here given code implementation process.

``````// C Program
// Check whether a number is positive or negative or zero
#include <stdio.h>

// Check status of given number
void numberSign(int num)
{
// Define possible result
const char *output[] = {
"Negative" , "Zero" , "Positive"
};
// status == 1 (then number is zero)
// status == 2 (then number is positive)
// status == 0 (then number is negative)
int status = ((num >> 31) - (-num >> 31)) + 1;
// Display given number
printf("\n Number : %d ", num);
// Display calculated result
printf("\n Result : %s\n", output[status]);
}
int main()
{
// Test case
numberSign(6);
numberSign(-6);
numberSign(0);
numberSign(12);
return 0;
}``````

#### Output

`````` Number : 6
Result : Positive

Number : -6
Result : Negative

Number : 0
Result : Zero

Number : 12
Result : Positive``````
``````/*
Java program
Check whether a number is positive or negative or zero
*/
public class Checker
{
// Check status of given number
public void numberSign(int num)
{
// Define possible result
String[] output = {
"Negative" , "Zero" , "Positive"
};
// status == 1 (then number is zero)
// status == 2 (then number is positive)
// status == 0 (then number is negative)
int status = ((num >> 31) - (-num >> 31)) + 1;
// Display given number
System.out.print("\n Number : " + num + " ");
// Display calculated result
System.out.print("\n Result : " + output[status] + "\n");
}
public static void main(String[] args)
{
// Test case
}
}``````

#### Output

`````` Number : 6
Result : Positive

Number : -6
Result : Negative

Number : 0
Result : Zero

Number : 12
Result : Positive``````
``````// Include header file
#include <iostream>
using namespace std;

/*
C++ program
Check whether a number is positive or negative or zero
*/

class Checker
{
public:
// Check status of given number
void numberSign(int num)
{
// Define possible result
string output[] = {
"Negative" , "Zero" , "Positive"
};
// status == 1 (then number is zero)
// status == 2 (then number is positive)
// status == 0 (then number is negative)
int status = ((num >> 31) - (-num >> 31)) + 1;
// Display given number
cout << "\n Number : " << num << " ";
// Display calculated result
cout << "\n Result : " << output[status] << "\n";
}
};
int main()
{
// Test case
return 0;
}``````

#### Output

`````` Number : 6
Result : Positive

Number : -6
Result : Negative

Number : 0
Result : Zero

Number : 12
Result : Positive``````
``````// Include namespace system
using System;
/*
C# program
Check whether a number is positive or negative or zero
*/
public class Checker
{
// Check status of given number
public void numberSign(int num)
{
// Define possible result
String[] output = {
"Negative" , "Zero" , "Positive"
};
// status == 1 (then number is zero)
// status == 2 (then number is positive)
// status == 0 (then number is negative)
int status = ((num >> 31) - (-num >> 31)) + 1;
// Display given number
Console.Write("\n Number : " + num);
// Display calculated result
Console.Write("\n Result : " + output[status] + "\n");
}
public static void Main(String[] args)
{
// Test case
}
}``````

#### Output

`````` Number : 6
Result : Positive

Number : -6
Result : Negative

Number : 0
Result : Zero

Number : 12
Result : Positive``````
``````<?php
/*
Php program
Check whether a number is positive or negative or zero
*/
class Checker
{
// Check status of given number
public	function numberSign(\$num)
{
// Define possible result
\$output = array("Negative", "Zero", "Positive");
// status == 1 (then number is zero)
// status == 2 (then number is positive)
// status == 0 (then number is negative)
\$status = ((\$num >> 31) - (-\$num >> 31)) + 1;
// Display given number
echo "\n Number : ". \$num;
// Display calculated result
echo "\n Result : ". \$output[\$status] ."\n";
}
}

function main()
{
// Test case
}
main();``````

#### Output

`````` Number : 6
Result : Positive

Number : -6
Result : Negative

Number : 0
Result : Zero

Number : 12
Result : Positive``````
``````/*
Node Js program
Check whether a number is positive or negative or zero
*/
class Checker
{
// Check status of given number
numberSign(num)
{
// Define possible result
var output = ["Negative", "Zero", "Positive"];
// status == 1 (then number is zero)
// status == 2 (then number is positive)
// status == 0 (then number is negative)
var status = ((num >> 31) - (-num >> 31)) + 1;
// Display given number
process.stdout.write("\n Number : " + num);
// Display calculated result
process.stdout.write("\n Result : " + output[status] + "\n");
}
}

function main()
{
// Test case
}
main();``````

#### Output

`````` Number : 6
Result : Positive

Number : -6
Result : Negative

Number : 0
Result : Zero

Number : 12
Result : Positive``````
``````#   Python 3 program
#   Check whether a number is positive or negative or zero

class Checker :
#  Check status of given number
def numberSign(self, num) :
#  Define possible result
output = ["Negative", "Zero", "Positive"]
#  status == 1 (then number is zero)
#  status == 2 (then number is positive)
#  status == 0 (then number is negative)
status = ((num >> 31) - (-num >> 31)) + 1
#  Display given number
print("\n Number : ", num, end = "")
#  Display calculated result
print("\n Result : ", output[status] )

def main() :
#  Test case

if __name__ == "__main__": main()``````

#### Output

`````` Number :  6
Result :  Positive

Number :  -6
Result :  Negative

Number :  0
Result :  Zero

Number :  12
Result :  Positive``````
``````#   Ruby program
#   Check whether a number is positive or negative or zero

class Checker
#  Check status of given number
def numberSign(num)
#  Define possible result
output = ["Negative", "Zero", "Positive"]
#  status == 1 (then number is zero)
#  status == 2 (then number is positive)
#  status == 0 (then number is negative)
status = ((num >> 31) - (-num >> 31)) + 1
#  Display given number
print("\n Number : ", num)
#  Display calculated result
print("\n Result : ", output[status] ,"\n")
end

end

def main()
#  Test case
end

main()``````

#### Output

`````` Number : 6
Result : Positive

Number : -6
Result : Negative

Number : 0
Result : Zero

Number : 12
Result : Positive
``````
``````/*
Scala program
Check whether a number is positive or negative or zero
*/
class Checker
{
// Check status of given number
def numberSign(num: Int): Unit = {
// Define possible result
var output: Array[String] = Array("Negative", "Zero", "Positive");
// status == 1 (then number is zero)
// status == 2 (then number is positive)
// status == 0 (then number is negative)
var status: Int = ((num >> 31) - (-num >> 31)) + 1;
// Display given number
print("\n Number : " + num);
// Display calculated result
print("\n Result : " + output(status) + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Checker = new Checker();
// Test case
}
}``````

#### Output

`````` Number : 6
Result : Positive

Number : -6
Result : Negative

Number : 0
Result : Zero

Number : 12
Result : Positive``````
``````/*
Swift 4 program
Check whether a number is positive or negative or zero
*/
class Checker
{
// Check status of given number
func numberSign(_ num: Int)
{
// Define possible result
let output: [String] = ["Negative", "Zero", "Positive"];
// status == 1 (then number is zero)
// status == 2 (then number is positive)
// status == 0 (then number is negative)
let status: Int = ((num >> 31) - (-num >> 31)) + 1;
// Display given number
print("\n Number : ", num, terminator: "");
// Display calculated result
print("\n Result : ", output[status] );
}
}
func main()
{
// Test case
}
main();``````

#### Output

`````` Number :  6
Result :  Positive

Number :  -6
Result :  Negative

Number :  0
Result :  Zero

Number :  12
Result :  Positive``````
``````/*
Kotlin program
Check whether a number is positive or negative or zero
*/
class Checker
{
// Check status of given number
fun numberSign(num: Int): Unit
{
// Define possible result
var output: Array < String > = arrayOf("Negative", "Zero", "Positive");
// status == 1 (then number is zero)
// status == 2 (then number is positive)
// status == 0 (then number is negative)
var status: Int = ((num shr 31) - (-num shr 31)) + 1;
// Display given number
print("\n Number : " + num);
// Display calculated result
print("\n Result : " + output[status] + "\n");
}
}
fun main(args: Array < String > ): Unit
{
// Test case
}``````

#### Output

`````` Number : 6
Result : Positive

Number : -6
Result : Negative

Number : 0
Result : Zero

Number : 12
Result : Positive``````

## Resultant Output Explanation

Based on the algorithm and given test cases, the program produces the following output:

``````Number : 6
Result : Positive

Number : -6
Result : Negative

Number : 0
Result : Zero

Number : 12
Result : Positive
``````

## Time Complexity:

The time complexity of the provided code is constant or O(1). It performs a fixed number of operations (bit manipulation, array indexing, and print statements) for each input. The execution time does not depend on the size of the input, making it a constant-time algorithm.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post