(1) Check If A Number Is Positive, Negative or 0.

num = float(input('Enter A Number '))
if num > 0:
    print('Positive Number')
elif num < 0:
    print('Negative Number')
else:
    print('Zero')
    

(2) Check If A Number Is Odd or Even.

num = float(input('Enter A Number'))
if num % 2 == 0:
    print('Even Number')
else:
    print('Odd Number')

(3) Check A Year Is Leap Year Or Not.

  • We have discussed above, we know that because the Earth rotates about 365.242375 times a year but a normal year is 365 days, something has to be done to “catch up” the extra 0.242375 days a year. 
  • So every fourth year we add an extra day (the 29th of February), which makes 365.25 days a year. This is fairly close but is wrong by about 1 day every 100 years.

  • So every 100 years we don’t have a leap year, and that gets us 365.24 days per year (1 day less in 100 years = -0.01 days per year). Closer, but still not accurate enough!

  • So another rule says that every 400 years is a leap year again, this gets us 365.2425 days per year (that is 1 day regained every 400 years equals 0.0025 days per year), which is close to 365.242375 not to matter much.

# Python program to check if year is a leap year or not

year = 2000

# To get year (integer input) from the user
# year = int(input("Enter a year: "))

# divided by 100 means century year (ending with 00)
# century year divided by 400 is leap year
if (year % 400 == 0) and (year % 100 == 0):
    print("{0} is a leap year".format(year))

# not divided by 100 means not a century year
# year divided by 4 is a leap year
elif (year % 4 ==0) and (year % 100 != 0):
    print("{0} is a leap year".format(year))

# if not divided by both 400 (century year) and 4 (not century year)
# year is not leap year
else:
    print("{0} is not a leap year".format(year))

(4) Find The Largest Among Three Numbers.

num1 = 10
num2 = 14
num3 = 12
largest = 0
if (num1>=num2) and (num1>=num3):
    largest = num1
elif (num2>=num1) and (num2>=num3):
    largest = num2
else:
    largest = num3
print(largest)
14

(5) Check A Number Is Prime Number Or Not.

  • A positive integer greater than 1 which has no other factors except 1 and the number itself is called a prime number. 2, 3, 5, 7 etc. are prime numbers as they do not have any other factors.

  • But 6 is not prime (it is composite) since, 2 x 3 = 6.

# Program to check if a number is prime or not

num = 29

# To take input from the user
#num = int(input("Enter a number: "))

# define a flag variable
flag = False

if num == 1:
    print(num, "is not a prime number")
elif num > 1:
    # check for factors
    for i in range(2, num):
        if (num % i) == 0:
            # if factor is found, set flag to True
            flag = True
            # break out of loop
            break

    # check if flag is True
    if flag:
        print(num, "is not a prime number")
    else:
        print(num, "is a prime number")
29 is a prime number

(6) Print All Prime Numbers In An Interval

  • We can use ‘for’ with ‘else’ block to do this operation.
  • The else keyword in a for loop specifies a block of code to be executed when the loop is finished.
  • The else block will NOT be executed if the loop is stopped by a break statement.
# Python program to display all the prime numbers within an interval

lower = 900
upper = 1000

print("Prime numbers between", lower, "and", upper, "are:")

for num in range(lower, upper + 1):
   # all prime numbers are greater than 1
   if num > 1:
       for i in range(2, num):
           if (num % i) == 0:
               break
       else:
           print(num)
Prime numbers between 900 and 1000 are:
907
911
919
929
937
941
947
953
967
971
977
983
991
997

(7) Find The Factorial of A Number

  • The factorial of a number is the product of all the integers from 1 to that number.
  • For example, the factorial of 6 is 1*2*3*4*5*6 = 720.
  • Factorial is not defined for negative numbers, and the factorial of zero is one0! = 1.
# Python program to find the factorial of a number provided by the user.

# change the value for a different result
num = 7

# To take input from the user
#num = int(input("Enter a number: "))

factorial = 1

# check if the number is negative, positive or zero
if num < 0:
   print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
   print("The factorial of 0 is 1")
else:
   for i in range(1,num + 1):
       factorial = factorial*i
   print("The factorial of",num,"is",factorial)

(8) Display The Multiplication Table

  • In the program below, we have used the for loop to display the multiplication table of 12.
# Multiplication table (from 1 to 10) in Python

num = 12

# To take input from the user
# num = int(input("Display multiplication table of? "))

# Iterate 10 times from i = 1 to 10
for i in range(1, 11):
   print(num, 'x', i, '=', num*i)
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120

(9) Python Program To Print Fibonacci Series.

  • A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8….
  • The first two terms are 0 and 1. 
  • All other terms are obtained by adding the preceding two terms. This means to say the nth term is the sum of (n-1)th and (n-2)th term.
# Program to display the Fibonacci sequence up to n-th term

nterms = int(input("How many terms? "))

# first two terms
n1, n2 = 0, 1
count = 0

# check if the number of terms is valid
if nterms <= 0:
   print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
   print("Fibonacci sequence upto",nterms,":")
   print(n1)
# generate fibonacci sequence
else:
   print("Fibonacci sequence:")
   while count < nterms:
       print(n1)
       nth = n1 + n2
       # update values
       n1 = n2
       n2 = nth
       count += 1
Enter A Positive Number10
0
1
1
2
3
5
8
13
21
34

Leave a Reply

Your email address will not be published. Required fields are marked *