0% found this document useful (0 votes)
12 views4 pages

Program

The document contains several Python programs including one for swapping two numbers without an intermediate variable, a basic calculator supporting addition, subtraction, multiplication, and division, a leap year checker, a Fibonacci series generator, a prime number checker, and a program to print a hollow pyramid pattern. Each program is designed to demonstrate different programming concepts and functionalities. The code includes user input handling and basic error checking.

Uploaded by

govindindolia90
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Program

The document contains several Python programs including one for swapping two numbers without an intermediate variable, a basic calculator supporting addition, subtraction, multiplication, and division, a leap year checker, a Fibonacci series generator, a prime number checker, and a program to print a hollow pyramid pattern. Each program is designed to demonstrate different programming concepts and functionalities. The code includes user input handling and basic error checking.

Uploaded by

govindindolia90
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

# program for swap two numbers without intermediate number

num1 = int(input("enter first number "))

num2 = int(input("enter second number "))

num1 = num1+num2

num2 = num1-num2

num1 = num1-num2

print(f"after swapping num 1 is {num1}")

print(f"after swaping num 2 is {num2}")

# program for calculator that support addition subtraction multiplication


division

def add(x,y):

return x+y

def sub(x,y):

return x-y

def multiply(x,y):

return x*y

def divide(x,y):

if y==0:

return "error"

return x/y

def calculator():

print( "basic calculator" )

print( "select opration" )

print( "1. add" )

print( "2. sub" )

print( "3. multiply" )

print( "4. divide" )


choice = input("enter choice (1/2/3/4):")

if choice in ("1","2","3","4"):

try:

num1= float(input("enter num 1:"))

num2= float(input("enter num 2:"))

except ValueError:

print("invalid input")

return

if choice == "1":

print(f"{num1}+{num2}={add(num1,num2)}")

elif choice=="2":

print(f"{num2}-{num2}={sub(num1,num2)}")

elif choice=="3":

print(f"{num2}*{num2}={multiply(num1,num2)}")

elif choice=="4":

print(f"{num2}/{num2}={divide(num1,num2)}")

else:

print("invalid input")

calculator()

# program to check leap year

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

if (year % 4 == 0):

if (year % 100 != 0) or (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.")

# Python program to print Fibonacci series up to n terms

n = int(input("Enter the number of terms: "))

a, b = 0, 1

if n <= 0:

print("Please enter a positive integer.")

elif n == 1:

print("Fibonacci sequence:")

print(a)

else:

print("Fibonacci sequence:")

for _ in range(n):

print(a, end=" ")

a, b = b, a + b

# Python program to check if a number is prime

num = int(input("Enter a number: "))

if num <= 1:

print(f"{num} is not a prime number.")

else:

for i in range(2, int(num**0.5) + 1):


if num % i == 0:

print(f"{num} is not a prime number.")

# Python program to print a hollow pyramid pattern

rows = 5

for i in range(1, rows + 1):

for j in range(rows - i):

print(" ", end="")

for k in range(1, 2 * i):

if k == 1 or k == 2 * i - 1 or i == rows:

print("*", end="")

else:

print(" ", end="")

print()

You might also like