top of page

3) Python Program to Check Leap Year

Leap Year:

A year is called a leap year if it contains an additional day which makes the number of the days in that year is 366. This additional day is added in February which makes it 29 days long.

A leap year occurred once every 4 years.

How to determine if a year is a leap year?

You should follow the following steps to determine whether a year is a leap year or not.

  1. If a year is evenly divisible by 4 means having no remainder then go to next step. If it is not divisible by 4. It is not a leap year. For example: 1997 is not a leap year.

  2. If a year is divisible by 4, but not by 100. For example: 2012, it is a leap year. If a year is divisible by both 4 and 100, go to next step.

  3. If a year is divisible by 100, but not by 400. For example: 1900, then it is not a leap year. If a year is divisible by both, then it is a leap year. So 2000 is a leap year.

See this example:

back.png

year = int(input("Enter a year: "))  

if (year % 4) == 0:  

   if (year % 100) == 0:  

       if (year % 400) == 0:  

           print("{0} is a leap year".format(year))  

       else:  

           print("{0} is not a leap year".format(year))  

   else:  

       print("{0} is a leap year".format(year))  

else:  

   print("{0} is not a leap year".format(year))  

4) Python Program to Check Prime Number

Prime numbers:

A prime number is a natural number greater than 1 and having no positive divisor other than 1 and itself.

examples: 3, 7, 11 etc are prime numbers.

Composite number:

Other natural numbers that are not prime numbers are called composite numbers.

example: 4, 6, 9 etc. are composite numbers.

See this example:

back.png

num = int(input("Enter a number: "))  

  

if num > 1:  

   for i in range(2,num):  

       if (num % i) == 0:  

           print(num,"is not a prime number")  

           print(i,"times",num//i,"is",num)  

           break  

   else:  

       print(num,"is a prime number")  

         

else:  

   print(num,"is not a prime number")  

bottom of page