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

Python Basic Calculator and Programs

The document outlines several Python programs demonstrating basic functionalities such as a calculator, checking odd/even numbers, summing odd numbers, finding the largest of three numbers, printing multiplication tables, and generating Fibonacci sequences. Each program includes the code, example results, and a step-by-step algorithm. The calculator program specifically handles operations like addition, subtraction, multiplication, and division, while also managing division by zero errors.

Uploaded by

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

Python Basic Calculator and Programs

The document outlines several Python programs demonstrating basic functionalities such as a calculator, checking odd/even numbers, summing odd numbers, finding the largest of three numbers, printing multiplication tables, and generating Fibonacci sequences. Each program includes the code, example results, and a step-by-step algorithm. The calculator program specifically handles operations like addition, subtraction, multiplication, and division, while also managing division by zero errors.

Uploaded by

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

Experiment 6

Building a basic calculator program using Python.

AIM:
To write a Python program that functions as a basic calculator capable of performing the following
operations: addition, subtraction, multiplication, and division.

Program

n1 = float(input("Enter the First Number: "))


n2 = float(input("Enter the Second Number: "))

#addition
print(f"{n1} + {n2} ={n1+n2}")

#subtraction
print(f"{n1} - {n2} = {n1-n2}")

#multiplication
print(f"{n1} * {n2} = {n1*n2}")

#division
print(f"{n1} / {n2} ={n1/n2} ")

Result 1
Enter the First Number: 2
Enter the Second Number: 1
2.0 + 1.0 =3.0
2.0 - 1.0 = 1.0
2.0 * 1.0 = 2.0
2.0 / 1.0 =2.0

Result 2

Enter the First Number: 8


Enter the Second Number: 0
8.0 + 0.0 =8.0
8.0 - 0.0 = 8.0
8.0 * 0.0 = 0.0
Traceback (most recent call last):
File "C:/Users/asus/AppData/Local/Programs/Python/Python313/[Link]", line 20, in
<module>
print(f"{n1} / {n2} ={n1/n2} ")
ZeroDivisionError: float division by zero
Algorithm

1. Start
2. Read the input and convert it to a floating-point number; store it in variable n1.
3. Read the input and convert it to a floating-point number; store it in variable n2.
4. Perform Addition:

Calculate n1 + n2 and display the result.

5. Perform Subtraction:

Calculate n1 - n2 and display the result.

6. Perform Multiplication:

Calculate n1 * n2 and display the result.

7. Perform Division:

Calculate n1 / n2 and display the result.

If n2=0 display a default error message: ZeroDivisionError: float division by zero

8. Stop
Experiment 7
Simple Programs using Python.

Program 1: Write a Python program to find whether the number given as user input is odd
or even

Program

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

if num % 2 == 0:

print(f" The number {num} is even ")

else:
print(f" The number {num} is odd ")

Result 1

Enter a number: 15
The number 15 is odd

Result 2

Enter a number: 24
The number 24 is even

Algorithm

1. Start
2. Read the input and convert it to an integer number; store it in variable num
3. Check if the number is even by using the modulo operator (number % 2 == 0).
4. If the condition is true, print that the number is even.
5. Otherwise, print that the number is odd.
6. Stop
Program 2: Write a Python program to find the sum of the first ten odd numbers

Program

sum = 0
for i in range(1, 20, 2):
sum = sum + i
print(i)
print(f"The sum of the first ten odd numbers is {sum}.")

Result
1
3
5
7
9
11
13
15
17
19
The sum of the first ten odd numbers is 100

Algorithm

1. Start

2. Initialize a variable sum to 0.

3. Use a loop that starts from 1, goes up to 20 (exclusive) with a step of 2 to generate odd
numbers.

4. In each iteration, add the current odd number to sum.

5. Print the current odd number.

6. After the loop, print the total sum.

7. Stop
Program 3: Write a Python program to find the largest number among the three numbers

Program

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


n2 = float(input("Enter the second number: "))
n3 = float(input("Enter the third number: "))
if (n1 >= n2 and n1 >= n3):
print(f"The largest number among {n1}, {n2}, and {n3} is {n1}.")
elif (n2 >= n1 and n2 >= n3):
print(f"The largest number among {n1}, {n2}, and {n3} is {n2}.")
else:
print(f"The largest number among {n1}, {n2}, and {n3} is {n3}.")

Result1
Enter the first number: 15.25
Enter the second number: 21.12
Enter the third number: 21.14
The largest number among 15.25, 21.12, and 21.14 is 21.14.

Result2
Enter the first number: -45
Enter the second number: -58
Enter the third number: -21
The largest number among -45.0, -58.0, and -21.0 is -21.0.

Algorithm

1. Start
2. Read three numbers: n1, n2, n3
3. If n1 is greater than or equal to n2 and n1 is greater than or equal to n3, then print n1 is the
largest.
4. Else, if n2 is greater than or equal to n1 and n2 is greater than or equal to n3, then print n2 is the
largest.
5. Else, print n3 is the largest.
6. Stop
Program 4: Write a Python program to print the multiplication table of 10 using loops.

Program

num = int(input("Enter a number to print its multiplication table: "))


for i in range(1, 11):
print(f"{num} x {i} = {num * i}")

Result
Enter a number to print its multiplication table: 10
10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100

Algorithm

1. Start
2. Read the input number from the user as num
3. For each value 'i' in the range from 1 to 10 (inclusive):
Multiply num by i and print the result in the format: num × i = result of number multiplied by
the respective i value
4. Stop
Program 5. Write the python program of finding Fibonacci sequence of n number

Program

nterms = int(input("Enter the last term "))


n1, n2 = 0, 1
while n1 < nterms:
print(n1)
n1,n2=n2, n1 + n2

Result 1
Enter the last term 10
0
1
1
2
3
5
8
Result 2
Enter the last term 25
0
1
1
2
3
5
8
13
21

Algorithm

1. Start by taking an integer input nterms from the user.


2. Initialize two variables n1 and n2 to 0 and 1 respectively.
3. While n1 is less than nterms:
4. print n1
5. Update n1 to n2 and n2 to the sum of the old n1 and n2 (using simultaneous
assignment).

You might also like