Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the GCD and LCM function in calculator. #45

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions Calculator/tkinter calculator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import parser
import tkinter.messagebox as msgbox # Added for displaying GCD and LCM results
from tkinter import *
root=Tk()
root.title("Calculator")
Expand All @@ -7,6 +8,12 @@
display.grid(row=1,columnspan=6,sticky=W+E)

#get the user input and place it in the textfield
# Input fields for two numbers
num1_entry = Entry(root)
num1_entry.grid(row=6, column=0)
num2_entry = Entry(root)
num2_entry.grid(row=6, column=2)


i=0
def get_variables(num):
Expand Down Expand Up @@ -44,6 +51,27 @@ def calculate():
clear_all()
display.insert(0,"error")

# Function to find the GCD of two numbers using Euclidean algorithm
def find_gcd(a, b):
while b:
a, b = b, a % b
return a

# Function to find the LCM of two numbers
def find_lcm(a, b):
return (a * b) // find_gcd(a, b)

# Calculate and display GCD and LCM
def calculate_gcd_lcm():
try:
num1 = int(num1_entry.get())
num2 = int(num2_entry.get())
gcd = find_gcd(num1, num2)
lcm = find_lcm(num1, num2)
msgbox.showinfo("Result", f"The GCD of {num1} and {num2} is: {gcd}\n"
f"The LCM of {num1} and {num2} is: {lcm}")
except ValueError:
msgbox.showerror("Error", "Please enter valid numbers for GCD and LCM calculation.")



Expand Down Expand Up @@ -78,8 +106,8 @@ def calculate():
Button(root,text="x!").grid(row=4,column=5)
Button(root,text="^2",command=lambda :get_operation('**2')).grid(row=5,column=5)



# Button to calculate GCD and LCM
Button(root, text="Calculate GCD/LCM", command=calculate_gcd_lcm).grid(row=6, column=4)



Expand Down