Program to check if a number is Positive, Negative, Odd, Even, Zero
Last Updated :
15 Feb, 2024
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`);
}
OutputPositive 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");
}
OutputPositive 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`);
}
OutputNegative 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));
Outputpositive, 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)
Similar Reads
Check if a number is positive, negative or zero using bit operators
Given a number N, check if it is positive, negative or zero without using conditional statements.Examples: Input : 30 Output : 30 is positive Input : -20 Output : -20 is negative Input: 0 Output: 0 is zero The signed shift n>>31 converts every negative number into -1 and every other into 0. Wh
5 min read
Prime Number Program | Code to Check whether a number is Prime or not
Given a positive integer n, write a code to check whether the number is prime. What is Prime Number?A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself. Examples of the first few prime numbers are {2, 3, 5, ...}Examples:Input: n = 11Output: trueInput: n =
12 min read
C++ program to print all Even and Odd numbers from 1 to N
Given a number N, the task is to print N even numbers and N odd numbers from 1. Examples: Input: N = 5 Output: Even: 2 4 6 8 10 Odd: 1 3 5 7 9 Input: N = 3 Output: Even: 2 4 6 Odd: 1 3 5 Approach: For Even numbers:Even numbers are numbers that are divisible by 2.To print even numbers from 1 to N, tr
4 min read
Check if a N base number is Even or Odd
Given a number num in base N, check whether it is even or odd.Examples: Input: num = 10, N = 8 Output: Even Explanation: 108 = 810, which is even Input: num = 122, N = 5 Output: Odd Explanation: 1225 = 3710, which is odd Approach: Convert the number num from Base N to Decimal base.Check whether the
6 min read
Check whether product of integers from a to b is positive , negative or zero
Given two integers a and b, the task is to check whether the product of integers from the range v[a, b] i.e. a * (a + 1) * (a + 2) * ... * b is positive, negative or zero. Examples: Input: a = -10, b = -2 Output: NegativeInput: a = -10, b = 2 Output: Zero Naive approach: We can run a loop from a to
7 min read
Check whether a given number is even or odd
Given a number n, check whether it is even or odd. Return true for even and false for odd.Examples: Input: 2 Output: trueInput: 5Output: falseApproach: By Finding the RemainderWe can check the remainder when divided by 2. If the remainder is 0, the number is even; otherwise, it is oddC++// A simple
5 min read
Check if an Octal number is Even or Odd
Given an Octal number N, check whether it is even or odd.Examples: Input: N = 7234 Output: Even Input: N = 333333333 Output: Odd Naive Approach: Convert the number from Octal base to Decimal base.Then check if the number is even or odd, which can be easily checked by dividing by 2. Time Complexity:
4 min read
Check if a Number is Odd or Even using Bitwise Operators
Given a number n, the task is to check whether the number is even or odd using Bitwise Operators.Examples: Input: n = 11 Output: OddInput: n = 10 Output: Even Following Bitwise Operators can be used to check if a number is odd or even:1. Using Bitwise XOR operator: The idea is to check whether the l
6 min read
Check whether given floating point number is even or odd
Given a floating-point number, check whether it is even or odd. We can check whether a integer is even or odd by dividing its last digit by 2. But in case of floating point number we can't check a given number is even or odd by just dividing its last digit by 2. For example, 100.70 is an odd number
6 min read
Check whether bitwise AND of N numbers is Even or Odd
Given an array arr[] containing N numbers. The task is to check whether the bitwise-AND of the given N numbers is even or odd.Examples: Input: arr[] = { 2, 12, 20, 36, 38 } Output: Even Input: arr[] = { 3, 9, 17, 13, 15 } Output: Odd A Simple Solution is to first find the AND of the given N numbers,
7 min read