Skip to main content

LCM/HCF calculator

 Hello! My name is "Nisanth" and welcome to my blog, Articklez! In this post, I'm going to show you how to make a simple LCM/HCF calculator. We will be using Python for this project. Go to "python.org" to install Python. Check out the below image for how to install it.



Open any text editor—such as Notepad, Notepad ++, Visual Studio Code, or Sublime Text—and start typing. In python, a bunch of code can be associated with a block based on the indentation. First, type the following text. 


print("LCM/HCF calculator")


This will simply display "LCM/HCF calculator". Now, to declare the variables. A "variable" is just a value that can be changed afterwards. They can be integers, text, lists, or boolean values (True/False); among others.


num1 = int(input("Enter the first number.\n"))

num2 = int(input("Enter the second number.\n"))


lcm = 0

hcf = 0


found = False


lcmOrHcf = input("Would you like to find the LCM or HCF?\n").upper()


In the first two declarations, we are using "int()" and "input()". "input()" allows the user to enter something. This is stored as text. "int()" converts numbers stored as text, to actual numbers. ".upper()" converts all the text to capital letters.


if lcmOrHcf == "LCM":

    if num2 > num1:

        counter = num2

    if num1 > num2:

        counter = num1

    while found == False:

        if (counter % num1 == 0) and (counter % num2 == 0):

            lcm = counter

            found = True

        else:

            counter += 1

    print("The LCM is " + str(lcm) + ".")


The first line is an "if" condition. If "lcmOrHcf"—the variable we declared—is "LCM", it sets "counter" to the smaller number. Then, the "while" loop we declared keeps running the code under it as long as "found" is False. If the counter is divisible by both numbers, it sets "lcm" to "counter" and "found" to True. If otherwise, it increases "counter" by 1. Once the loop is over (indicating that "found" is True), it displays the LCM. "str()" converts the LCM to text because "print()" can only display text.


elif lcmOrHcf == "HCF":

    if num2 > num1:

        counter = num1

    if num1 > num2:

        counter = num2

    while found == False:

        if (num1 % counter == 0) and (num2 % counter == 0):

            hcf = counter

            found = True

        else:

            counter -= 1

    print("The HCF is " + str(hcf) + ".")





The first line is an "elif" condition. If the previous condition is false and this condition is true, it runs the code. If "lcmOrHcf"—the variable we declared—is "HCF", it sets "counter" to the larger number. If the both numbers are divisible by "counter", it sets "hcf" to "counter" and "found" to True. If otherwise, it decreases "counter" by 1. Once the loop is over (indicating that "found" is True), it displays the HCF. "str()" converts the HCF to text because "print()" can only display text. It's complete! To view the output, in your cmd/terminal, open the directory and type "python " and the filename. The below pictures should elaborate.

Here is the output.





Thank you!

Comments

Popular posts from this blog

The Ramayana in a Nutshell

Image Credits: Blog - I Think Solutions     One day the great sage, Valmiki saw a pair of Krauncha birds. He was filled with joy on seeing them. But, his glee was short-lived, for, a hunter immediately killed the pair of birds. Valmiki was furious. He cursed the hunter in the ancient language, Sanskrit. But, his curse somehow seemed kind of poetic. Surprised, Valmiki returned back to his ashram. Suddenly, Brahma (the creator) appeared. He gave Valmiki the power to see Lord Rama's past, present, and future; and asked Valmiki to compose a book on him. He composed the poem and taught it to Lava and Kusha, who were Rama's sons living in Valmiki's ashram. They recited the poem in many places. It so happened that Rama himself heard of the boys and asked them to sing it in his court. They began: Bala Kand: During the Dwapara and the Treta yugas, injustice prevailed. To rid the earth of all evil, everyone requested Vishnu (the protector) to be incarnate on the earth. He was born ...

Monotony — by N1SPAR

An easy to sing, song about the journey of the Indian tenth grader through their board exams. I wrote this song in the January preceding my board exams (where I scored 98%, by the way), and sang this in my school's farewell party. When it comes to my rap, I call myself "N1SPAR" (short for "Nisanth Parupalli"). Credits Written by Nisanth Parupalli Produced and performed by N1SPAR Lyrics (Yeah!) The monotony,  no anatomy, Desperation won't saddle me! Like a bike with no handles, wee! 'Cause nobody can handle me! Time to learn your "A", "B", "C"s. Then, move on to your "1", "2", "3"s. Education starts when you're three, But the real deal's when you're fifteen! Get a ninety-eight, they'll shave yo' head. It's like a war and you're out for blood! Read stuff you've already read— Read until you go to bed! "E" stands for "E—X—A—M", Wake up at 5:10 am! ...

Jailbreak

Hello! My name is Nisanth and welcome to my blog, Articklez. This article is about a game I made, called "Jailbreak". I made it using a tool called "Construct 2". Construct 2 was developed by a startup called "Scirra". It is quite easy to develop games using it. This is one such game which only works on a laptop/desktop. Jailbreak is a game where there's an imaginary outlaw on the run. He first has to escape the cops' dynamite-filled prison, then he has to race his way to the finish line to escape. But, that's not it! In the second level, when the user escapes the prison and is engaged in driving to the finish, there are cop cars chasing him! This is a fun, interactive, and tactical game. Here's a video to demonstrate: