0% found this document useful (0 votes)
15 views1 page

Number Type Checker for Real and Complex

Uploaded by

kavyadlncsahai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views1 page

Number Type Checker for Real and Complex

Uploaded by

kavyadlncsahai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

def check_number_type(num):

if isinstance(num, complex):
if [Link] == 0 and [Link] == 0:
return "Neutral (Zero)"
else:
return "Neither positive nor negative (complex number)"
else:
if num > 0:
return "Positive"
elif num < 0:
return "Negative"
else:
return "Neutral (Zero)"

# Input from the user


user_input = input("Enter a number (real or complex): ")

# Try to convert the input into a complex number first


try:
number = complex(user_input)
except ValueError:
print("Invalid input. Please enter a valid number.")
else:
result = check_number_type(number)
print(f"The number you entered is: {result}")

How It Works:
1. Input Handling: The program takes input from the user and tries to convert it into
a complex number.
2. Type Checking: The program checks if the number is of type complex using
isinstance().
– For complex numbers:
• If both the real and imaginary parts are zero, it is considered “Neutral
(Zero)”.
• If not, the program outputs that the number is neither positive nor
negative, as these concepts don’t directly apply to complex numbers.
– For real numbers:
• Positive if the number is greater than zero.
• Negative if the number is less than zero.
• Neutral if the number is exactly zero.
3. Output: The program prints whether the number is positive, negative, or neutral.
This approach ensures that the program correctly handles both real and complex numbers.

You might also like