Q.Python Program to find the area of triangle.
a = int(input(“enter 1st side of the triangle”))
b = int(input(“enter 2nd side of the triangle”))
c = int(input(“enter 3rd side of the triangle”))
s = (a+b+c)/2
print(“area is” , s*(s-a)*(s-b)*(s-c))
Output:
enter 1st side of the triangle3
enter 2nd side of the triangle4
enter 3rd side of the triangle5
area is 6.0
Q.Python Program to calculate the square root.
a = int(input(“Enter a number to get its square root”))
sqrt=a**0.5
print(sqrt)
Output:
Enter a number to get its square root16
4.0
QPython program to swap two variables.
x = int(input(“enter value of x: “))
y = int(input(“enter value of y: “))
a=x
x=y
y=a
print(“x is”, x , “and y is” , y)
Output:
enter value of x: 5
enter value of y: 4
x is 4 and y is 5
Q.Program to find Greatest between two Numbers.
x = int(input(“enter 1st number : “))
y= int(input(“enter 2nd number : “))
if x>=y:
print(x , ‘is greater than’ , y)
elif x==y:
print(x , ‘is equal to’ , y)
else:
print(x , ‘is less than’ , y)
Output:
enter 1st number : 5
enter 2nd number : 10
5 is less than 10
Q.Program to check whether the given Year is Leap Year.
x = int(input(‘enter year to check if its a leap year: ‘))
if x%4==0 and x%!=100:
print(‘its a leap year’)
else:
print(‘it is not a leap year’)
Output:
enter year to check if its a leap year: 2004
its a leap year
Q.Program to Calculate Grade.
x = int(input(‘enter marks out of 100 to get its grade: ‘))
if x<=100 and x>=0:
if x>=90:
print(‘the student is quite smart he got an A’)
elif x>=70:
print(‘you got a B, try better next time’)
elif x>=50:
print(‘you got a C’)
elif x>=33:
print(‘you just passed and you got a D’)
else:
print(‘you failed’)
else:
print(‘enter valid marks’)
Output:
enter marks out of 100 to get its grade: 90
the student is quite smart he got an A
>
Q Program to find greatest among Three Numbers.
x = int(input(‘enter a number: ’))
y = int(input(‘enter a number: ’))
z = int(input(‘enter a number: ’))
if x>y and x>z:
print(x , ’is the greatest integer’)
elif y>x and y>z:
print(y , ‘is the greatest integer’)
elif z>x and z>y:
print(z , ‘is the greatest integer’)
else:
print(‘make sure you enter distinct numbers’)
Output:
enter a number: 10
enter a number: 20
enter a number: 30
30 is the greatest integer
Q.Program to check whether the no. is Even or Odd
x= int(input(‘enter number to check if its odd or even’))
if x%2==0:
print(‘even number’)
else:
print(‘odd number’)
Output:
enter number to check if its odd or even: 6
even number
Q.Program to Calculate A to the power B.
x = int(input(‘enter the base’))
y = int(input(‘enter the power’))
print(x**y)
Output:
enter the base3
enter the power2
Q.Program to print Fibonacci Series 0, 1, 1, 2, 3, 5, 8, 13…….
x=0
y=1
while x>=0:
z=x+y
x=y
y=z
print(z)
Output:
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040
1346269
2178309
3524578
5702887
9227465
14930352
24157817
39088169
…….
Q. Program to print Multiplication Table of a Given Numbers
x = int(input(‘enter a number’))
i=0
while i<=0:
print(x*i)
i=i+1
Output:
enter a number5
10
15
20
25
30
35
40
45
50
>
Q. Program to check whether the given no. is Prime or not.
x =int(input('enter a number'))
i=2
while i in range(2,x):
i=i+1
if x%i==0:
print('prime')
break
else:
print('not prime')
break
Output:
enter a number5
not prime
Q. Program to Calculate Factorial of a given Number
x = int(input(‘enter a number’))
y=1
i=1
while i in range(i,x+1):
y=y*i
i = i+1
print(y)
Output:
enter a number5
120
Q. Program to print First 10 Natural Numbers in reverse Order.
i=10
while i in range(1,11):
print (i)
i=i-1
Output:
10
1
Q.Program to find smallest among given numbers
x = int(input(“enter a number”))
y = int(input(“enter a number”))
z = int(input(“enter a number”))
if x<y and x<z:
print(x)
elif y<z and y<x:
print(y)
elif z<y and z<x:
print(z)
else:
print(“all numbers are equal”)
Output:
enter a number3
enter a number4
enter a number5
3
Q.Program to check whether the given number is an Armstrong.
#Explanation of my logic: This is a complex piece of code so i will
explain this code here. When a user enters a number to check if its
an armstrong number what i will need to do is to get the number of
digits in it to check it. For that we will have to use logarithms. we
write log10(100) = 2 but no. of digits in 100 is 3. Similarly
log10(12345)=4.09149 converting to integer we get 4 now digits in
12345 is 5.
#we see here that no of digits is int(log10(number)) + 1
using this we can calculate no of digits of of any no entered by the
user.
import math
sum= 0
i=1
x = int(input(‘enter a number to check it its a armstrong no: ’))
digits = math.floor(math.log10(x)) + 1
while x>0
digit
Q.Program to print series of Prime Numbers between 1 to 100
and
Q. Show use of break and continue
print(2)
number = 3
i=2
while number in range(1,100):
if number%i==0:
break
else:
i=i+1
print(‘not a prime number’)
Q Program to find sum of 1+2+3+4+……N
x = int(input(‘enter number’))
sum = x*(x+1)/2
print(sum)
Output:
enter number5
15.0
Q Program to find (1)+(1+2)+(1+2+3)+……n
x = int(input(‘enter number’))
sum = x*(x+1)*(x+2)/6
print(sum)
Output:
enter number4
20.0
Q Program to display
A B
A B C
A B C D
for i in range(4):
for j in range(i + 1):
print(chr(65 + j), end=" ")
print()
Output:
AB
ABC
ABCD
Q WAP TO CALCULATE SERIES 1/1^2 + 1/3^2 + ....... 1/N^2
x = int(input(‘enter number’))
sum = 1/(4*x*(x+1)*(x-1)/3 + x)
print(sum)
Output:
enter number2
0.1
Q Wap to swap two numbers without taking help of third variable.
x = int(input(‘enter number’))
y = int(input(‘enter number’))
print(x)
print(y)
x = x+y
y = x-y
x = x-y
print(‘swapped’)
print(x)
print(y)
Output:
enter number5
enter number10
10
swapped
10
Q Wap to find LCM and HCF of two numbers.
x = int(input(‘enter a number’))
y = int(input(‘enter a number’))
r = x%y
while r
Q WAP TO CALCULATE SERIES 1+ x1/2!+ x2/3!+.........xn/(n+1)!
import math
n = int(input(‘enter no. of digits’))
sum = (1 - 1/(math.factorial(n+1)))
print(sum)
enter no. of digits5
0.9986111111111111
>
Q.Wap to guess a number using random module and print “u won “ if
guessed number is same as your inputted number otherwise print
“you lost “
import random
x = math.randint(0,10)
print(‘guess from 1 to 10’)
y = int(input(‘enter your guess’))
if x==y:
print(‘you won’)
else:
print(‘you lost’)
Output:
guess from 1 to 10
enter your guess3
you lost
Q. Write a program to reverse a given string
x = input('Enter a string to reverse: ')
length = len(x)
i=1
print(x)
while i <= length:
print(x[-i], end=''’’)
i=i+1
Output:
Enter a string to reverse: abcd
abcd
dcba
Q. Write a program to capitalise the given string
x =(input(‘enter a string ’))
print(x.upper())
output:
enter a string abcd
ABCD
Q WAP to right shift all elements of a list
def right_shift(lst):
if not lst:
return lst
shifted = [0] * len(lst)
for i in range(len(lst)):
shifted[(i + 1) % len(lst)] = lst[i]
return shifted
Output:
original = [1, 2, 3, 4, 5]
shifted = right_shift(original)
print("Original:", original)
print("Shifted:", shifted)
Q WAP to delete all occurencs in a list
def remove_occurrences(lst, char):
return [c for c in lst if c != char]
original = ['a', 'b', 'c', 'a', 'd', 'e', 'a']
char_to_remove = 'a'
filtered = remove_occurrences(original, char_to_remove)
print("Original:", original)
print("Filtered:", filtered)
Output:
Original: ['a', 'b', 'c', 'a', 'd', 'e', 'a']
Filtered: ['b', 'c', 'd', 'e']
Q WAP to square all even nos and cube all odd nos in a list entered
by a user.
# Function to square even numbers and cube odd numbers
def transform_numbers(numbers):
transformed_numbers = []
for num in numbers:
if num % 2 == 0:
transformed_numbers.append(num ** 2) # Square even
numbers
else:
transformed_numbers.append(num ** 3) # Cube odd
numbers
return transformed_numbers
# Input from the user
user_input = input("Enter a list of numbers separated by spaces: ")
user_numbers = [int(num) for num in user_input.split()]
# Transform the numbers
transformed_numbers = transform_numbers(user_numbers)
# Display the transformed numbers
print("Original Numbers:", user_numbers)
print("Transformed Numbers:", transformed_numbers)
Enter a list of numbers separated by spaces: 1 2 3 4 5
Original Numbers: [1, 2, 3, 4, 5]
Transformed Numbers: [1, 4, 27, 16, 125]
def find_frequency(numbers):
frequency_dict = {} # Initialize an empty dictionary to store
frequencies
for num in numbers:
if num in frequency_dict:
frequency_dict[num] += 1 # Increment the count if the
number is already in the dictionary
else:
frequency_dict[num] = 1 # Add the number to the
dictionary if it's not already present
return frequency_dict
# Input from the user
user_input = input("Enter a list of numbers separated by spaces: ")
user_numbers = [int(num) for num in user_input.split()]
# Find the frequency of numbers
frequency_result = find_frequency(user_numbers)
# Display the frequency results
print("Frequency of Numbers:")
for num, count in frequency_result.items():
print(f"{num}: {count}")
Enter a list of numbers separated by spaces: 1 2 3 2 4 3 5 2 1
Frequency of Numbers:
1: 2
2: 3
3: 2
4: 1
5: 1
Q wap to input a 2d list and display it
def input_2d_list(rows, columns):
matrix = []
for i in range(rows):
row = []
for j in range(columns):
element = input(f"Enter element at position ({i}, {j}): ")
row.append(element)
matrix.append(row)
return matrix
def display_2d_list(matrix):
for row in matrix:
print(" ".join(row))
# Input the dimensions of the 2D list from the user
rows = int(input("Enter the number of rows: "))
columns = int(input("Enter the number of columns: "))
# Input the 2D list
print("Input the elements of the 2D list:")
matrix = input_2d_list(rows, columns)
# Display the 2D list
print("\n2D List:")
display_2d_list(matrix)
Output:
Enter the number of rows: 2
Enter the number of columns: 3
Input the elements of the 2D list:
Enter element at position (0, 0): 1
Enter element at position (0, 1): 2
Enter element at position (0, 2): 3
Enter element at position (1, 0): 4
Enter element at position (1, 1): 5
Enter element at position (1, 2): 6
2D List:
123
456