Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 92 - 2629_Devagiri_C-7
P. 92

    Program 4 To check whether a number is odd or even.

                          Program4.py
                       File  Edit  Format   Run    Options   Window    Help

                       Num=int(input("Enter a Number: "))
                       if Num%2==0:
                         print("Number entered is an even number.")
                       else:
                         print("Number entered is an odd number.")

                          Output

                       Enter a Number: 12
                       Number entered is an even number.
                       Enter a Number: 7
                       Number entered is an odd number.

                      This program takes an input Num as an integer, divides it by 2 and compares the remainder with
                      0. If it is found equal, the number is even, otherwise the program prints that the number is odd.

                      NESTED IF STATEMENT
                      The nested if structure allows you to place one if statement inside another. This means the
                      program first checks a main condition and if that condition is true, it checks another condition

                      within it.
                        Program 5 To check if a number is positive or negative and, if so, check whether it is even or odd.

                          Program5.py
                       File  Edit  Format   Run    Options   Window    Help

                       num = int(input("Enter a number: "))
                       if num > 0:
                           print("You have entered a positive number.")
                           if num % 2 == 0:
                               print("Even")
                           else:
                               print("Odd")
                       else:
                           print("You have entered a negative number.")
                           if num % 2 == 0:
                               print("Even")
                           else:
                               print("Odd")

                       90
                            Premium Edition-VII

| 87 88 89 90 91 92 93 94 95 96 97 |