Simple python programs on numbers: To check the number is odd or even and positive or negative.

             Numbers are always magical. Basic python programs include simple logic. some of the examples are given below.

  • ·       Python program to find the given number is odd or even
  • ·       Python program to check the given number is positive or negative.
  • ·       Python program to find biggest of three numbers.
  • ·       Python program to find the given number is prime or not.

Python program to find the given number is odd or even:

              It uses an arithmetic operator (%). This operator gives you the remainder from the division. Here, we divide the number by 2. If the remainder is zero, then given number is even. Otherwise, it is a odd number.

#As a beginning, get the number by calling a method ‘input’. Typecast the input into integer.

no = int(input(“Enter the number”))

# ‘%’ - divide the number by 2 and get the reminder. If the reminder is zero, then it goes to the loop.

if no % 2 == 0 :

 # Prints the number is even.

    print(“It is an even number”)

#otherwise, it prints the number is odd.

else :

    print(“It is an odd number”)

Just execute this program in interpreter or save it into a file “oddeven.py”. Run it in the command prompt. You will get the output.


Python program to find the given number is positive or negative:

#Get the number from command prompt.

no = int(input(“Enter the number”))

#check the number is greater than zero. If yes, print it is a positive number

if no > 0 :

    print(“It is a positive number”)

#otherwise, print it is a negative number.

else :

    print(“It is a negative number”)

run this program in any python compiler. You will get the following output.

Output:


Programs to find the given number is prime or not and  biggest of three numbers is in next blog post.

No comments:

Post a Comment