0% found this document useful (0 votes)
4 views

1 Week Programs

The document contains three Python programs: one to determine if a year is a leap year, another to calculate the greatest common divisor (GCD) and least common multiple (LCM) of two integers, and a calculator application that performs basic arithmetic operations. Each program includes user input prompts and outputs the results accordingly. The code snippets demonstrate fundamental programming concepts such as functions, conditionals, and user interaction.

Uploaded by

diperman013
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

1 Week Programs

The document contains three Python programs: one to determine if a year is a leap year, another to calculate the greatest common divisor (GCD) and least common multiple (LCM) of two integers, and a calculator application that performs basic arithmetic operations. Each program includes user input prompts and outputs the results accordingly. The code snippets demonstrate fundamental programming concepts such as functions, conditionals, and user interaction.

Uploaded by

diperman013
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

a) Program to determine whether a given year is a leap year

def is_leap_year(year):

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):

return True

else:

return False

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

if is_leap_year(year):

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

else:

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

b) Program to determine the greatest common divisor (GCD) and least


common multiple (LCM) of two integers

import math

def gcd(a, b):

return math.gcd(a, b)

def lcm(a, b):

return abs(a * b) // math.gcd(a, b)

num1 = int(input("Enter the first integer: "))

num2 = int(input("Enter the second integer: "))

gcd_value = gcd(num1, num2)

lcm_value = lcm(num1, num2)


print(f"The GCD of {num1} and {num2} is: {gcd_value}")

print(f"The LCM of {num1} and {num2} is: {lcm_value}")

c) Program to create a calculator application


def calculator():

print("Enter an operation in the format: N1 OP N2")

n1 = float(input("Enter the first number (N1): "))

op = input("Enter the operator (one of: +, -, *, /, %, **): ")

n2 = float(input("Enter the second number (N2): "))

if op == '+':

result = n1 + n2

elif op == '-':

result = n1 - n2

elif op == '*':

result = n1 * n2

elif op == '/':

if n2 != 0:

result = n1 / n2

else:

return "Error! Division by zero."

elif op == '%':

result = n1 % n2

elif op == '**':

result = n1 ** n2

else:

return "Invalid operator!"

return f"The result of {n1} {op} {n2} is: {result}"

You might also like