Saturday, December 28, 2024
Google search engine
HomeLanguagesProgram to check if a number is Positive, Negative, Odd, Even, Zero

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 Even

Input : 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`);
}


Output

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");
}


Output

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:

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))


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`);
}


Output

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:

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)) 


Output

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.

RELATED ARTICLES

Most Popular

Recent Comments