If Statement Example Programs


Check the given number is even or odd using if statement in python

This code prompts the user to input a number and stores it as an integer. Then it checks if the number is even by using the modulus operator(%) to check if the remainder when the number is divided by 2 is equal to 0. If the remainder is 0, it means the number is even and will print "The given number is Even". If the remainder is not 0, it means the number is odd and will print "The given number is Odd".

Source Code

number =int(input("Enter a number :"))
if(number %2 == 0):
    print("The given number is Even")
else:
    print("The given number is Odd")
To download raw file Click Here

Output

Enter a number :7
The given number is Odd

Check the given year is leap year or not using if statement in python

This code prompts the user to input a year and stores it as an integer. Then it checks if the year is a leap year by using the modulus operator(%) to check if the remainder when the year is divided by 4 is equal to 0. If the remainder is 0, it means the year is a leap year and will print "The given year is leap year". If the remainder is not 0, it means the year is not a leap year and will print "The given year is not a leap year". However, this is not a complete rule for leap year, A leap year is a year that is evenly divisible by 4, except for end-of-century years which must be divisible by 400.

Source Code

year =int(input("Enter a year: "))
if(year % 4 == 0):
    print("The given year is leap year")
else:
    print("The given year is not a leap year")
To download raw file Click Here

Output

Enter a year: 2002
The given year is not a leap year