Program to check if a number is Positive, Negative, Odd, Even, Zero
Given a number n, the task is to check whether the given number is positive, negative, odd, even, or zero.
Method 1 :
- A number is positive if it is greater than zero. We check this in the expression of if.
- If it is False, the number will either be zero or negative.
- This is also tested in subsequent expressions.
- In the case of odd and even A number is even if it is perfectly divisible by 2.
-
- When the number is divided by 2, we use the remainder operator % to compute the remainder.
- If the remainder is not zero, the number is odd.
Examples:
Input : 10
Output : Positive number10 is EvenInput : 0
Output : 0 is Even
C++
#include <iostream> using namespace std; int main() { int num = 10; // Checking for positive, negative or zero if (num > 0) { cout << "Positive number" << endl; } else if (num == 0) { cout << "Zero" << endl; } else { cout << "Negative number" << endl; } // Checking for odd and even if (num % 2 == 0) { cout << num << " is Even" << endl; } else { cout << num << " is Odd" << endl; } return 0; } |
Java
public class Main { public static void main(String[] args) { int num = 10 ; // Checking for positive, negative or zero if (num > 0 ) { System.out.println( "Positive number" ); } else if (num == 0 ) { System.out.println( "Zero" ); } else { System.out.println( "Negative number" ); } // Checking for odd and even if (num % 2 == 0 ) { System.out.println(num + " is Even" ); } else { System.out.println(num + " is Odd" ); } } } |
Python
# Python Code to check if a number is # Positive, Negative, Odd, Even, Zero # Using if...elif...else num = 10 if num > 0 : print ( "Positive number" ) elif num = = 0 : print ( "Zero" ) else : print ( "Negative number" ) # Checking for odd and even if (num % 2 ) = = 0 : print ( "{0} is Even" . format (num)) else : print ( "{0} is Odd" . format (num)) |
C#
using System; class Program { static void Main( string [] args) { int num = 10; // Checking for positive, negative or zero if (num > 0) { Console.WriteLine( "Positive number" ); } else if (num == 0) { Console.WriteLine( "Zero" ); } else { Console.WriteLine( "Negative number" ); } // Checking for odd and even if (num % 2 == 0) { Console.WriteLine(num + " is Even" ); } else { Console.WriteLine(num + " is Odd" ); } } } |
Javascript
// JavaScript Code to check if a number is // Positive, Negative, Odd, Even, Zero // Using if...else if...else let num = 10; if (num > 0) { console.log( "Positive number" ); } else if (num === 0) { console.log( "Zero" ); } else { console.log( "Negative number" ); } // Checking for odd and even if (num % 2 === 0) { console.log(`${num} is Even`); } else { console.log(`${num} is Odd`); } |
Positive number 10 is Even
Time complexity: The time complexity of the above code is O(1), as it takes only one step to complete the operation.
Space complexity: The space complexity of the above code is O(1), as no extra space is required in order to complete the operation.
Method 2:
C++
#include <iostream> using namespace std; int main() { int num = 20; // Checking for positive, negative or zero if (num > 0) { cout << "Positive number" << endl; } else if (num == 0) { cout << "Zero" << endl; } else { cout << "Negative number" << endl; } // Checking for odd and even if (num % 2 == 0) { cout << num << " is Even" << endl; } else { cout << num << " is Odd" << endl; } return 0; } |
Java
/*package whatever //do not write package name here */ import java.io.*; class GFG { public static void main (String[] args) { int num = 20 ; // Checking for positive, negative or zero if (num > 0 ) { System.out.println( "Positive number" ); } else if (num == 0 ) { System.out.println( "Zero" ); } else { System.out.println( "Negative number" ); } // Checking for odd and even if (num % 2 == 0 ) { System.out.println( num + " is Even" ); } else { System.out.println( num + " is Odd" ); } } } |
Python
# Python Code to check if a number is # Positive, Negative, Odd, Even, Zero # Using Nested if num = 20 if num > = 0 : if num = = 0 : print ( "Zero" ) else : print ( "Positive number" ) else : print ( "Negative number" ) # Checking for odd and even if (num % 2 ) = = 0 : print ( "{0} is Even" . format (num)) else : print ( "{0} is Odd" . format (num)) |
C#
using System; public class Program { public static void Main() { int num = 20; // Checking for positive, negative or zero if (num > 0) { Console.WriteLine( "Positive number" ); } else if (num == 0) { Console.WriteLine( "Zero" ); } else { Console.WriteLine( "Negative number" ); } // Checking for odd and even if (num % 2 == 0) { Console.WriteLine(num + " is Even" ); } else { Console.WriteLine(num + " is Odd" ); } } } |
Javascript
let num = 20; // Checking for positive, negative, or zero if (num > 0) { console.log( "Positive number" ); } else if (num === 0) { console.log( "Zero" ); } else { console.log( "Negative number" ); } // Checking for odd and even if (num % 2 === 0) { console.log(num + " is Even" ); } else { console.log(num + " is Odd" ); } |
Positive number 20 is Even
Time Complexity: The time complexity of the above program is O(1), as the maximum number of comparisons occurring in the program is constant.
Space Complexity: The space complexity of the above program is O(1), as no extra space is required for the program to run.
Method 3:
C++
#include <iostream> #include <string> using namespace std; int main() { int num = -10; // Convert the integer 'num' to a string for analysis. string strNum = to_string(num); // Check if the string representation starts with a '-' sign. if (strNum[0] == '-' ) { cout << "Negative number" << endl; } // If it doesn't start with '-', check if it's "0". else if (strNum == "0" ) { cout << "Zero" << endl; } // If it's neither negative nor zero, it's a positive number. else { cout << "Positive number" << endl; } // Check if 'num' is even or odd by calculating the remainder when divided by 2. if (num % 2 == 0) { cout << num << " is Even" << endl; } else { cout << num << " is Odd" << endl; } return 0; } |
Java
public class Main { public static void main(String[] args) { int num = - 10 ; String strNum = Integer.toString(num); if (strNum.startsWith( "-" )) { System.out.println( "Negative number" ); } else if (strNum.equals( "0" )) { System.out.println( "Zero" ); } else { System.out.println( "Positive number" ); } if (num % 2 == 0 ) { System.out.println(num + " is Even" ); } else { System.out.println(num + " is Odd" ); } } } |
Python3
# Python Code to check if a number is # Positive, Negative, Odd, Even, Zero # Using if...elif...else num = - 10 x = str (num) if x.startswith( "-" ): print ( "Negative number" ) elif x = = "0" : print ( "Zero" ) else : print ( "Positive number" ) # Checking for odd and even if (num % 2 ) = = 0 : print ( "{0} is Even" . format (num)) else : print ( "{0} is Odd" . format (num)) |
C#
using System; class Program { static void Main() { int num = -10; // Convert the integer 'num' to a string for analysis. string strNum = num.ToString(); // Check if the string representation starts with a '-' sign. if (strNum[0] == '-' ) { Console.WriteLine( "Negative number" ); } // If it doesn't start with '-', check if it's "0". else if (strNum == "0" ) { Console.WriteLine( "Zero" ); } // If it's neither negative nor zero, it's a positive number. else { Console.WriteLine( "Positive number" ); } // Check if 'num' is even or odd by calculating the remainder when divided by 2. if (num % 2 == 0) { Console.WriteLine($ "{num} is Even" ); } else { Console.WriteLine($ "{num} is Odd" ); } } } |
Javascript
const num = -10; const strNum = num.toString(); if (strNum.startsWith( "-" )) { console.log( "Negative number" ); } else if (strNum === "0" ) { console.log( "Zero" ); } else { console.log( "Positive number" ); } if (num % 2 === 0) { console.log(`${num} is Even`); } else { console.log(`${num} is Odd`); } |
Negative number -10 is Even
Time complexity: O(1) as it is doing constant operations
Auxiliary Space: O(1) as it is using constant space for variables
Method 4:
One approach to check if a number is positive, negative, odd, even, or zero without using if-else statements is to use the built-in functions abs, divmod, and isinstance.
Here is an example of how these functions can be used:
C++
#include <iostream> #include <cmath> class CheckNumber { public : // Function to check the sign, odd/even, and zero/non-zero status of a number static std::string checkNumber( int num) { std::string sign, oddEven, zero; // Check the sign of the number if (std:: abs (num) == num) { sign = "positive" ; } else { sign = "negative" ; } // Check if the number is odd or even if (num % 2 == 0) { oddEven = "even" ; } else { oddEven = "odd" ; } // Check if the number is zero or non-zero if (num == 0) { zero = "zero" ; } else { zero = "non-zero" ; } // Combine the results and return return sign + ", " + oddEven + ", " + zero; } }; int main() { // Test cases std::cout << CheckNumber::checkNumber(5) << std::endl; // positive, odd, non-zero std::cout << CheckNumber::checkNumber(-5) << std::endl; // negative, odd, non-zero std::cout << CheckNumber::checkNumber(6) << std::endl; // positive, even, non-zero return 0; } |
Java
public class CheckNumber { public static String checkNumber( int num) { String sign, oddEven, zero; if (Math.abs(num) == num) { sign = "positive" ; } else { sign = "negative" ; } if (num % 2 == 0 ) { oddEven = "even" ; } else { oddEven = "odd" ; } if (num == 0 ) { zero = "zero" ; } else { zero = "non-zero" ; } return sign + ", " + oddEven + ", " + zero; } public static void main(String[] args) { System.out.println(checkNumber( 5 )); // positive, odd, non-zero System.out.println(checkNumber(- 5 )); // negative, odd, non-zero System.out.println(checkNumber( 6 )); // positive, even, non-zero } } |
Python3
def check_number(num): if abs (num) = = num: sign = "positive" else : sign = "negative" if divmod (num, 2 )[ 1 ] = = 0 : odd_even = "even" else : odd_even = "odd" if isinstance (num, int ) and num = = 0 : zero = "zero" else : zero = "non-zero" return f "{sign}, {odd_even}, {zero}" print (check_number( 5 )) # positive, odd, non-zero print (check_number( - 5 )) # negative, odd, non-zero print (check_number( 6 )) |
C#
using System; public class CheckNumber { // Function to check the sign, odd/even, and zero/non-zero status of a number public static string CheckNumberStatus( int num) { string sign, oddEven, zero; // Check the sign of the number if (Math.Abs(num) == num) { sign = "positive" ; } else { sign = "negative" ; } // Check if the number is odd or even if (num % 2 == 0) { oddEven = "even" ; } else { oddEven = "odd" ; } // Check if the number is zero or non-zero if (num == 0) { zero = "zero" ; } else { zero = "non-zero" ; } // Combine the results and return return $ "{sign}, {oddEven}, {zero}" ; } } class Program { static void Main() { // Test cases Console.WriteLine(CheckNumber.CheckNumberStatus(5)); // positive, odd, non-zero Console.WriteLine(CheckNumber.CheckNumberStatus(-5)); // negative, odd, non-zero Console.WriteLine(CheckNumber.CheckNumberStatus(6)); // positive, even, non-zero } } |
Javascript
function checkNumber(num) { let sign, oddEven, zero; // Check if the number is positive or negative if (Math.abs(num) === num) { sign = "positive" ; } else { sign = "negative" ; } // Check if the number is odd or even if (num % 2 === 0) { oddEven = "even" ; } else { oddEven = "odd" ; } // Check if the number is zero or non-zero if (Number.isInteger(num) && num === 0) { zero = "zero" ; } else { zero = "non-zero" ; } return `${sign}, ${oddEven}, ${zero}`; } console.log(checkNumber(5)); console.log(checkNumber(-5)); console.log(checkNumber(6)); |
positive, odd, non-zero negative, odd, non-zero positive, even, non-zero
The time complexity for this algorithm is O(1), since it does not rely on the size of the input. It only needs to check the properties of the number once and then return the output accordingly.
The space complexity is also O(1), as the algorithm does not use any additional memory, and only returns a constant output.
the abs function returns the absolute value of a number, so abs(num) would return the positive version of num regardless of whether it is positive or negative. The divmod function returns a tuple containing the quotient and remainder of dividing the first argument by the second argument, so divmod(num, 2)[1] would return the remainder of dividing num by 2. The isinstance function returns True if the first argument is an instance of the type specified by the second argument, and False otherwise. In the example above, isinstance(num, (int, float)) would return True if num is an integer or a float, and False otherwise.
Method 5 :
One approach to check if a number is positive, negative, odd, even, or zero without using if-else statements is to use the built-in functions typeid.
C++
#include <iostream> #include <typeinfo> // Include the typeinfo header std::string check_number( int num) { std::string sign, odd_even, zero; if ( abs (num) == num) { sign = "positive" ; } else { sign = "negative" ; } if (num % 2 == 0) { odd_even = "even" ; } else { odd_even = "odd" ; } if ( typeid (num) == typeid ( int ) && num == 0) { zero = "zero" ; } else { zero = "non-zero" ; } return sign + ", " + odd_even + ", " + zero; } int main() { std::cout << check_number(5) << std::endl; // positive, odd, non-zero std::cout << check_number(-5) << std::endl; // negative, odd, non-zero std::cout << check_number(6) << std::endl; // positive, even, non-zero return 0; } |
Java
public class NumberChecker { // Method to check properties of a number public static String checkNumber( int num) { String sign, oddEven, zero; // Determine if the number is positive or negative if (Math.abs(num) == num) { sign = "positive" ; } else { sign = "negative" ; } // Check if the number is odd or even if (num % 2 == 0 ) { oddEven = "even" ; } else { oddEven = "odd" ; } // Determine if the number is zero or non-zero if (num == 0 ) { zero = "zero" ; } else { zero = "non-zero" ; } // Return a combination of properties return sign + ", " + oddEven + ", " + zero; } // Main method to test the checkNumber function public static void main(String[] args) { // Test cases with sample numbers System.out.println(checkNumber( 5 )); // positive, odd, non-zero System.out.println(checkNumber(- 5 )); // negative, odd, non-zero System.out.println(checkNumber( 6 )); // positive, even, non-zero } } |
Python3
def check_number(num): sign = "positive" if abs (num) = = num else "negative" odd_even = "even" if num % 2 = = 0 else "odd" zero = "zero" if isinstance (num, int ) and num = = 0 else "non-zero" return f "{sign}, {odd_even}, {zero}" if __name__ = = "__main__" : print (check_number( 5 )) # positive, odd, non-zero print (check_number( - 5 )) # negative, odd, non-zero print (check_number( 6 )) # positive, even, non-zero |
C#
using System; class Program { // Method to check properties of a number static string CheckNumber( int num) { string sign, oddEven, zero; // Check if the number is positive or negative if (Math.Abs(num) == num) { sign = "positive" ; } else { sign = "negative" ; } // Check if the number is odd or even if (num % 2 == 0) { oddEven = "even" ; } else { oddEven = "odd" ; } // Check if the number is zero or non-zero if (num.GetType() == typeof ( int ) && num == 0) { zero = "zero" ; } else { zero = "non-zero" ; } // Combine the properties and return as a string return sign + ", " + oddEven + ", " + zero; } static void Main() { // Test cases for the CheckNumber method Console.WriteLine(CheckNumber(5)); // positive, odd, non-zero Console.WriteLine(CheckNumber(-5)); // negative, odd, non-zero Console.WriteLine(CheckNumber(6)); // positive, even, non-zero } } |
Javascript
// Function to check properties of a number function checkNumber(num) { let sign, oddEven, zero; // Check if the number is positive or negative if (Math.abs(num) === num) { sign = "positive" ; } else { sign = "negative" ; } // Check if the number is odd or even if (num % 2 === 0) { oddEven = "even" ; } else { oddEven = "odd" ; } // Check if the number is zero or non-zero if ( typeof num === 'number' && num === 0) { zero = "zero" ; } else { zero = "non-zero" ; } // Combine the properties and return as a string return sign + ", " + oddEven + ", " + zero; } // Test cases for the checkNumber function console.log(checkNumber(5)); // positive, odd, non-zero console.log(checkNumber(-5)); // negative, odd, non-zero console.log(checkNumber(6)); // positive, even, non-zero |
Output :
positive, odd, non-zero
negative, odd, non-zero
positive, even, non-zero
Time Complexity : O (1 )
Space Complexity : O(1)