0% found this document useful (0 votes)
3 views9 pages

Python Assignment

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)
3 views9 pages

Python Assignment

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

Py_assignment

Name: Geedikapally Karuna


Roll no:21D41A0573
Branch: cse
Mail: geedikapallykaruna3@[Link]

1)write a program to print n natural numbers in reverse


order.
Program:
n=int(input("enter the value of n:"))
for i in range(n,0,-1):
print(i)
Expected output:
enter the value of n:10
10
9
8
7
6
5
4
3
2
1
2)write a program to make a calculator.
Program:
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
if y == 0:
return "Cannot divide by zero!"
return x / y
while True:
print("Options:")
print("Enter 'add' for addition")
print("Enter 'subtract' for subtraction")
print("Enter 'multiply' for multiplication")
print("Enter 'divide' for division")
print("Enter 'exit' to end the program")
user_input = input(": ")
if user_input == "exit":
break
if user_input in ("add", "subtract", "multiply", "divide"):
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

if user_input == "add":
print("Result:", add(num1, num2))
elif user_input == "subtract":
print("Result:", subtract(num1, num2))
elif user_input == "multiply":
print("Result:", multiply(num1, num2))
elif user_input == "divide":
print("Result:", divide(num1, num2))
else:
print("Invalid input. Please enter a valid operation.")
Expected output:
Options:
Enter 'add' for addition
Enter 'subtract' for subtraction
Enter 'multiply' for multiplication
Enter 'divide' for division
Enter 'exit' to end the program
: add
Enter first number: 2
Enter second number: 2
Result: 4.0
Options:
Enter 'add' for addition
Enter 'subtract' for subtraction
Enter 'multiply' for multiplication
Enter 'divide' for division
Enter 'exit' to end the program
: subtract
Enter first number: 4
Enter second number: 2
Result: 2.0
Options:
Enter 'add' for addition
Enter 'subtract' for subtraction
Enter 'multiply' for multiplication
Enter 'divide' for division
Enter 'exit' to end the program
: multiply
Enter first number: 3
Enter second number: 8
Result: 24.0
Options:
Enter 'add' for addition
Enter 'subtract' for subtraction
Enter 'multiply' for multiplication
Enter 'divide' for division
Enter 'exit' to end the program
: exit
3)write a program to print the multiplication table in
reverse order.
Program:
number = 5
start = 10
end = 1
step = -1
for i in range(start, end - 1, step):
result = number * i
print(f"{number} x {i} = {result}")
Expected output:
5 x 10 = 50
5 x 9 = 45
5 x 8 = 40
5 x 7 = 35
5 x 6 = 30
5 x 5 = 25
5 x 4 = 20
5 x 3 = 15
5 x 2 = 10
5 x 1 = 5
4)Find the given number is even or odd without using
arithmetic operator.
Program:
def is_even_or_odd(num):
return "Even"
else:
return "Odd"
number1 = 24
number2 = 19
print(f"{number1} is {is_even_or_odd(number1)}")
print(f"{number2} is {is_even_or_odd(number2)}")
Expected output:
24 is Even
19 is Odd

5)write a program to reverse a given number with excluding


odd numbers.
Program:
def reverse_number_excluding_odd(number):
num_str = str(number)
reversed_str = ""
for digit in num_str[::-1]:
if int(digit) % 2 == 0:
reversed_str += digit
reversed_num = int(reversed_str)
return reversed_num
input_number = 456789101112
result = reverse_number_excluding_odd(input_number)
print(f"Original Number: {input_number}")
print(f"Reversed Number (excluding odd digits): {result}")
Expected output:
Original Number: 456789101112
Reversed Number (excluding odd digits): 20864

6)python program to calculate Factorial using recursion.


Program:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
num = int(input("Enter a non-negative integer: "))
if num < 0:
print("Factorial is undefined for negative numbers.")
else:
result = factorial(num)
print(f"{num}! = {result}")
Expected output:
Enter a non-negative integer: 9
9! = 362880

Py2_assignment
1)write a program to print all the owels in the string by the
concatination.
Program:
def print_vowels(string):
vowels = "AEIOUaeiou"
result = ""
for char in string:
if char in vowels:
result += char
print("Concatenation of vowels in the string:", result)
input_string = input("Enter a string: ")
print_vowels(input_string)
Expected output:
Enter a string: karuna
Vowels in the string: aua

2)write a program to print all the amstrong numbers


provided range given by the user.
Program:
def is_armstrong(number):
num_str = str(number)
num_digits = len(num_str)
sum_of_cubes = sum(int(digit) ** num_digits for digit in num_str)
return number == sum_of_cube
start = int(input("Enter the start of the range: "))
end = int(input("Enter the end of the range: "))
if start >= end:
print("Invalid range. The start should be less than the end.")
else:
print(f"Armstrong numbers in the range {start} to {end}:")
for num in range(start, end + 1):
if is_armstrong(num):
print(num)
Expected output:
Enter the start of the range: 6
Enter the end of the range: 14
Armstrong numbers in the range 6 to 14:
6
7
8
9

3)write a program to reverse a given number with excluding


odd numbers.
Program:
def reverse_number_exclude_odd(number):
num_str = str(number)
reversed_num_str = ""
for digit in num_str:
digit_value = int(digit)
if digit_value % 2 == 0:
reversed_num_str = str(digit_value) + reversed_num_str
reversed_num = int(reversed_num_str)
return reversed_num
number = int(input("Enter a number: "))
result = reverse_number_exclude_odd(number)
print("Reversed number with odd digits excluded:", result)
Expected output:
Enter a number: 456734
Reversed number with odd digits excluded: 464
4) Write a program to create a secured auto-password out
of the given person details. (Ex: First two letters from name,
last three numbers of their phone number, middle letters in
mail, reverse of first three letters).
Input:

name: Avinash
Contact Number: 9701923501
mail= avinash12@[Link]
Address: Vijayawada
Output:
Av401najiV
Program:
def generate_auto_password(name, phone_number, email):
name_part = name[:2]
phone_part = phone_number[-3:]
email_parts = [Link]('@')
if len(email_parts) == 2:
email_part = email_parts[0][1:-1]
else:
email_part = ''
reverse_name_part = name[:3][::-1]
auto_password = name_part + phone_part + email_part +
reverse_name_part
return auto_password
name = "Avinash"
phone_number = "9701923501"
email = "avinash12@[Link]"
secured_password = generate_auto_password(name,
phone_number, email)
print(secured_password)
Expected output:
Av501vinash1ivA
5) A chocolate factory is packing chocolates into the
packets. The chocolate packets here represent an array of
N number of integer values. The task is to find the empty
packets(0) of chocolate and push it to the end of the
conveyor belt(array).
Example 1 :
N=7 and arr = [4,5,0,1.9,0,5,0].
There are 3 empty packets in the given set. These 3 empty
packets represented as 0 should be pushed towards the end
of the array
Input :
7 – Value of N
[4,5,0,1,0,0,5] – Element of arr[O] to arr[N-1],While input
each element is separated by newline
Output:
4515000
Program:
def move_empty_packets(arr)
n = len(arr)
non_empty_count = 0
for i in range(n):
if arr[i] != 0:
arr[non_empty_count] = arr[i]
non_empty_count += 1
while non_empty_count < n:
arr[non_empty_count] = 0
non_empty_count += 1
N = int(input("Enter the value of N: "))
arr = []
print("Enter elements of arr:")
for _ in range(N):
element = int(input())
[Link](element)
move_empty_packets(arr)
print("Output:")
for element in arr:
print(element, end=" ")
Expected output:
Enter the value of N: 7
Enter elements of arr:
4
5
0
1
0
0
5
Output:
4515000

You might also like