Thursday, April 22, 2021

[Solved] Program To Check Leap Year In Python Using Functions


In this tutorial, we will learn how to calculate leap year with a simple python script. First, we need to understand the rules behind a year being a leap year to write the code. The steps to calculate leap year are following


  1. If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.

  2. If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.

  3. If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.

  4. The year is a leap year (it has 366 days).

  5. The year is not a leap year (it has 365 days).


For more information and detailed examples, we can read This article. 


To write a python script, we will create a flowchart of the rules as mentioned earlier.


Leap_Year_Check_Python_Flowchart



After creating a flowchart, we can start writing python code.


Step 1: Ask User for Input

To allow users to input a value, we need to add an 'input' statement in our program. The 'input' statement displays a string and returns the text input by users, so we need to store it in a variable. We will define a variable named 'user_input', so our first statement becomes this.


user_input = input(“"Enter a Year: ")


However, the input statement returns any value input by the user as text. To handle this, we add the 'int' statement before the 'input' statement.



user_input = int(input(“"Enter a Year: "))


Step 2: Process the Input

Once the user has input, we have our value stored in a variable name 'user_input' we can process it as 'user_input'. First, we divide the 'user_input' by 4. Please keep in mind that we will use the modulus division method because we want to check if the remainder is 0 or not.

 If the remainder is 0, then we divide it by 100. However, before we check the remainder of the division of 100, if we re-read Rule 2 mentioned above, we can see if we do not check the remainder of the division of 100 for 0. However, for 1, we can skip step 3 and directly go to step 4. So our code becomes



if (year%4==0):

        if(year%100==1):

            print(f"{year} is a leap year!!") 



**notice the indentation? Keep it that way because python uses indentation to nest statements.


We have used two 'if' statements to check the leap year, but when both the 'if' statements are false, we will divide 'user_input' by 400 and check the remainder for 0. We add 'else statement for this purpose.


def leap_check(year):


    if (year%4==0):

        if(year%100==1):

            print(f"{year} is a leap year!!")    

        else:

            if(year%400==0):

                print(f"{year} is a leap year!!")


Then we declare everything else as not a leap year.



if (year%4==0):

        if(year%100==1):

            print(f"{year} is a leap year!!")    

        else:

            if(year%400==0):

                print(f"{year} is a leap year!!")

            else:

                print(f"{year} is not a leap year!!")

    else:

        print(f"{year} is not a leap year!!")



Now wrap up all the code in a function and call it in main.


def leap_check(year):


    if (year%4==0):

        if(year%100==1):

            print(f"{year} is a leap year!!")    

        else:

            if(year%400==0):

                print(f"{year} is a leap year!!")

            else:

                print(f"{year} is not a leap year!!")

    else:

        print(f"{year} is not a leap year!!")



Step 3: Display outputs

The 'print' function will display the and data already used in the 'if' statement.


user_year = int(input("Enter a Year: "))

leap_check(user_year)


If you find any difficulties in this code or tutorial, please click here or visit Ask For Help.


#Source Code 👇
#-------------------------------------------------------------------------------------
#Leap Year Check 

def leap_check(year):

    if (year%4==0):
        if(year%100==1):
            print(f"{year} is a leap year!!")    
        else:
            if(year%400==0):
                print(f"{year} is a leap year!!")
            else:
                print(f"{year} is not a leap year!!")
    else:
        print(f"{year} is not a leap year!!")


print("\t\t\n\nWelcome!! This programme checks if input year is leap year\n\n")


user_year = int(input("Enter a Year: "))
leap_check(user_year)

#-----------------------------------------------------------------------

OUTPUT 👇
Welcome!! This programme checks if input year is leap year Enter a Year: 2000 2000 is a leap year!!

Dont forget to share your feedback in comments section


Output of Leap Year Check Python Flowchart



Wednesday, April 21, 2021

How to create BMI Calculator in Python [Source Code]


If you are a beginner and getting started with python programming it is recommended to write small python codes to understand the basics, instead of viewing videos for solutions.


Here, we try to accomplish a simple task by writing a Body Mass Index Calculator in Python Script. For this purpose we need to understand what a Body Mass Index Calculator is. If you google what is Body Mass Index you are gonna get the following definition.


“an approximate measure of whether someone is over- or underweight, calculated by dividing their weight in kilograms by the square of their height in metres.”

 

 

Thats simple you calculate your weight with respect to your height to determine whether you are overweight or not. Now that we have understood what body mass index (BMI) is, we need to know the formula of it to implement it in the python programming language. The formula is following

 

BMI = Weight / Height²

 

 

Keep in mind that weight is in Kilograms and Height is in Meters. Users must know this before entering values.

 

Next thing we should do is to tell the user their Body Mass Index with attributes (overweight, underweight or normal) again, to do that we should know the range of value of  attributes. These are the range of Body Mass Index (BMI)

 

BMI weight ranges

  • Less than 18.5 = Underweight

  • Between 18.5 - 24.9 = Healthy Weight

  • Between 25 - 29.9 = Overweight

  • Over 30 = Obese

  • Over 35 = Critically Obese

 

INFORMATION SOURCE

 

Now we create a flow chart for our python script it is not necessary but very useful

 

BMI Calculator In Python Programming Language

 

 

 

To begin our python script we start by a welcome message which tells a user about functionality of our program.

 

print("\n\n\t\tBMI Calculator Using Functions\n\n")

 

Next, ask user to input values. In python if we want a user to input a value we simply use the “input()” function which returns text value and to perform mathematical division we need the user input value to be an integer so we add “int()” function before it. Remember that in python language the “input()” function returns a value so we will need a variable to store it. As shown in flowchart we create a variable named “Weight” and same flow goes for “Height”

 

weight = int(input("Enter your weight in KG: "))

height = float(input("Enter your height in Meters: "))

 

 

That’s it. In python programming language if we need to make decisions “we use if else,” but in this python script we will use “if elif and else” because we have multiple values to check another main thing which should be followed strictly is that we always start from lowest value by doing this we avoid doing excessive if’s and elif’s. Simply write

 

 if BMI < 18.5:

        print(f"Your BMI is {BMI}, you are underweight")

    elif BMI < 25:

        print(f"Your BMI is {BMI}, you have normal weight")

    elif BMI < 30:

        print(f"Your BMI is {BMI}, you are overweight")

    elif BMI < 35:

        print(f"Your BMI is {BMI}, your are obese")

    else:

        print(f"Your BMI is {BMI}, you are clinically obese")

 

 

Our python program is finished here but to simplify we will add if’s into a function

 

def BMI_Calculator(user_weight, user_height):

    BMI = round(weight/(height*height),2)

    if BMI < 18.5:

        print(f"Your BMI is {BMI}, you are underweight")

    elif BMI < 25:

        print(f"Your BMI is {BMI}, you have normal weight")

    elif BMI < 30:

        print(f"Your BMI is {BMI}, you are overweight")

    elif BMI < 35:

        print(f"Your BMI is {BMI}, your are obese")

    else:

        print(f"Your BMI is {BMI}, you are clinically obese")    

 

Feel free to copy the code and share your feedback.

Program Source Code 👇 | Select and Press CTRL + C to copy the code

#BMI Calculator 2.0
def BMI_Calculator(user_weightuser_height):
    BMI = round(weight/(height*height),2)
    if BMI < 18.5:
        print(f"Your BMI is {BMI}, you are underweight")
    elif BMI < 25:
        print(f"Your BMI is {BMI}, you have normal weight")
    elif BMI < 30:
        print(f"Your BMI is {BMI}, you are overweight")
    elif BMI < 35:
        print(f"Your BMI is {BMI}, your are obese")
    else:
        print(f"Your BMI is {BMI}, you are clinically obese")    

print("\n\n\t\tBMI Calculator Using Functions\n\n")

weight = int(input("Enter your weight in KG: "))
height = float(input("Enter your height in Meters: "))


BMI_Calculator(user_weight=weight, user_height=height)



Program Output « 👇

                BMI Calculator Using Functions

  

Enter your weight in KG: 45

Enter your height in Meters: 1.1

Your BMI is 37.19, you are clinically obese


BMI calculator output in python programming language




Thursday, December 24, 2020

Assembly Language Program To Find Even And Odd Numbers In 8086


This program takes user input as an array and then determines the number of even numbers and prints them. This program is written using 8086 assembly language using emu8086 software.




TITLE PUCHTAA 
.MODEL SMALL 
.STACK 100H 
.DATA 
    MSG1 DB 0AH, 0DH, "THIS PROGRAM ACCEPTS SIZE OF ARRAY AND ELEMENTS THEN PRINTS EVEN NUMBERS$" 
    MSG DB 0AH, 0DH, "ENTER A LENGHT OF ARRAY: $" 
    ARRAY DB 0AH, 0DH, "ENTER ELEMENTS OF ARRAY$" 
    EVEN DB 0AH, 0DH, "TOTAL EVEN NUMBERS ARE $" 
    CONTINUE DB 0AH, 0DH, "CONTINUE? [Y/N] $" 
    NOEVEN DB 0AH, 0DH, "NO EVEN NUMBERS IN ARRAY!!$" 
 
 
 
 
.CODE 
    MAIN: 
    MOV AX, @DATA 
    MOV DS, AX 
     
    LEA DX, MSG1 
    MOV AH, 09H 
    INT 21H 
    CONT:    
    LEA DX, MSG 
    MOV AH, 09H 
    INT 21H 
     
    MOV AH, 01H 
    INT 21H 
     
    SUB AL, 30H 
    MOV CL, AL 
     
    LEA DX, ARRAY 
    MOV AH, 09H 
    INT 21H 
     
    MOV CH, 00H 
     
    AGAIN: 
    MOV DL, ' ' 
    MOV AH, 02H 
    INT 21H 
     
    MOV AH, 01H 
    INT 21H 
     
    SUB AL, 30H 
    MOV AH, 00H 
    MOV BL, 02H 
    AAD 
    DIV BL 
    CMP AH, 00H 
    JE INCREASE 
    RETURN: 
     
    DEC CL 
    CMP CL, 00H 
    JG AGAIN 
     
    CMP CH, 00H 
    JE NOEVENS 
     
    LEA DX, EVEN 
    MOV AH, 09H 
    INT 21H 
     
    JMP NUM 
     
 NOEVENS: 
    LEA DX, NOEVEN 
    MOV AH, 09H 
    INT 21H 
    JMP QUEST 
     
    
 NUM: 
     
    MOV DL, ' ' 
    MOV AH, 02H 
    INT 21H 
    
    POP AX 
    MOV DL, AH 
    ADD DL, 30H 
    MOV AH, 02H 
    INT 21H 
    DEC CH 
    CMP CH, 00H 
    JNE NUM 
 QUEST:    
    LEA DX, CONTINUE 
    MOV AH, 09H 
    INT 21H 
     
    MOV AH, 01H 
    INT 21H 
    OR AL, 20H 
    CMP AL, 'y' 
    JE CONT 
     
    JMP EXIT 
     
     
     
     
     
     
    INCREASE: 
    MOV AH, AL 
    ADD AH, AL 
    PUSH AX 
    INC CH 
    JMP RETURN 
     
    EXIT: 
    MOV AH, 4CH 
    INT 21H 
    END MAIN 

Finding Even Numbers In 8086 Assembly


Complete Video Tutorials