UNIT-1
1.Write
a program to find the largest element
among three Numbers.
# The following code finds the Largest of Three
Unique Numbers using if-elif-else
# Taking user input
num1 = int(input("Enter a first number : "))
num2 = int(input("Enter a second number : "))
num3 = int(input("Enter a third number : "))
# Condition checking
if num1>num2 and num1>num3:
print(f"First number ({num1}) is the largest.")
elif num2>num1 and num2>num3:
print(f"Second number ({num2}) is the largest.")
else:
print(f"Third number ({num3}) is the largest.")
Output
Enter a first number : 3
Enter a second number : 54
Enter a third number : 32
Second number (54) is the largest.
2. Write a Program to display all prime numbers within an
interval
To print all the prime numbers between the given interval, the user has to follow the
following steps:
o Step 1: Loop through all the elements in the given range.
o Step 2: Check for each number if it has any factor between 1 and itself.
o Step 3: If yes, then the number is not prime, and it will move to the next number.
o Step 4: If no, it is the prime number, and the program will print it and check for
the next number.
o Step 5: The loop will break when it is reached to the upper value.
# First, we will take the input:
lower_value = int(input ("Please, Enter the Lowest Range Value: "))
upper_value = int(input ("Please, Enter the Upper Range Value: "))
print ("The Prime Numbers in the range are: ")
for number in range (lower_value, upper_value + 1):
if number > 1:
for i in range (2, number):
if (number % i) == 0:
break
else:
print (number)
Output:
Please, Enter the Lowest Range Value: 14
Please, Enter the Upper Range Value: 97
The Prime Numbers in the range are:
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
3. Write a program to swap two numbers without using a
temporary variable.
# Python code to swap two numbers
# using + and - operators
x = 5.4
y = 10.3
print ("Before swapping: ")
print("Value of x : ", x, " and y : ", y)
# Swap code
x = x + y # x = 15.7, y = 10.3
y = x - y # x = 15.7, y = 5.4
x = x - y # x = 10.3, y = 5.4
print ("After swapping: ")
print("Value of x : ", x, " and y : ", y)
Output:
Before swapping:
Value of x : 5.4 and y : 10.3
After swapping:
Value of x : 10.3 and y : 5.4
5. Write a program to add and multiply complex numbers
print("Addition of two complex numbers : ",(4+3j)+(3-7j))
print("Subtraction of two complex numbers : ",(4+3j)-(3-7j))
print("Multiplication of two complex numbers : ",(4+3j)*(3-7j))
print("Division of two complex numbers : ",(4+3j)/(3-7j))
Sample Output:
Addition of two complex numbers : (7-4j)
Subtraction of two complex numbers : (1+10j)
Multiplication of two complex numbers : (33-19j)
Division of two complex numbers : (-
0.15517241379310348+0.6379310344827587j)
6. Write a program to print multiplication table of a given
number.
numb = int(input(" Enter a number : "))
# using the for loop to generate the multiplication tables
print("Table of: ")
for a in range(1,11):
print(num,'x',a,'=',num*a)
Output
Enter the number : 7
Multiplication Table of :
7x1=7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70