Skip to main content

Check whether a number is positive or negative or zero

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)
	{
		Checker task = new Checker();
		// Test case  
		task.numberSign(6);
		task.numberSign(-6);
		task.numberSign(0);
		task.numberSign(12);
	}
}

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()
{
	Checker task = Checker();
	// Test case
	task.numberSign(6);
	task.numberSign(-6);
	task.numberSign(0);
	task.numberSign(12);
	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)
	{
		Checker task = new Checker();
		// Test case
		task.numberSign(6);
		task.numberSign(-6);
		task.numberSign(0);
		task.numberSign(12);
	}
}

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()
{
	$task = new Checker();
	// Test case
	$task->numberSign(6);
	$task->numberSign(-6);
	$task->numberSign(0);
	$task->numberSign(12);
}
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()
{
	var task = new Checker();
	// Test case
	task.numberSign(6);
	task.numberSign(-6);
	task.numberSign(0);
	task.numberSign(12);
}
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() :
	task = Checker()
	#  Test case  
	task.numberSign(6)
	task.numberSign(-6)
	task.numberSign(0)
	task.numberSign(12)

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() 
	task = Checker.new()
	#  Test case  
	task.numberSign(6)
	task.numberSign(-6)
	task.numberSign(0)
	task.numberSign(12)
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
		task.numberSign(6);
		task.numberSign(-6);
		task.numberSign(0);
		task.numberSign(12);
	}
}

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()
{
	let task: Checker = Checker();
	// Test case
	task.numberSign(6);
	task.numberSign(-6);
	task.numberSign(0);
	task.numberSign(12);
}
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
{
	var task: Checker = Checker();
	// Test case
	task.numberSign(6);
	task.numberSign(-6);
	task.numberSign(0);
	task.numberSign(12);
}

Output

 Number : 6
 Result : Positive

 Number : -6
 Result : Negative

 Number : 0
 Result : Zero

 Number : 12
 Result : Positive




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.

New Comment