0% found this document useful (0 votes)
2 views

Shivam Python Last

The document contains a series of Python programming assignments completed by Shivam Negi, a BCA student. Each problem statement includes a specific task such as checking for palindromes, Armstrong numbers, calculating factorials, generating Fibonacci sequences, and handling lists and dictionaries. The document provides code snippets along with sample outputs for each problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Shivam Python Last

The document contains a series of Python programming assignments completed by Shivam Negi, a BCA student. Each problem statement includes a specific task such as checking for palindromes, Armstrong numbers, calculating factorials, generating Fibonacci sequences, and handling lists and dictionaries. The document provides code snippets along with sample outputs for each problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 15

NAME: SHIVAM NEGI

COURSE – BCA 4 ‘C1’


ROLL NO-68

PROBLEM STATEMENT 1: Write a python program to check if a value entered


by a user is Palindrome or not.

num=int(input("Enter a number:"))
temp=num
rev=0
while(num>0):
dig=num%10
rev=rev*10+dig
num=num//10
if(temp==rev):
print("The number is palindrome!")
else:
print("Not a palindrome!")

Output:

Enter a number:121

The number is palindrome!


NAME: SHIVAM NEGI
COURSE – BCA 4 ‘C1’
ROLL NO.:68

PROBLEM STATEMENT 2: Write a python program to check if a value


entered by a user is Armstrong or not.

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

sum = 0

temp = num

while temp > 0:

digit = temp % 10

sum += digit ** 3

temp //= 10

if num == sum:

print(num,"is an Armstrong number")

else:

print(num,"is not an Armstrong number")

Output:

Enter a number: 153

153 is an Armstrong number


NAME: SHIVAM NEGI
COURSE – BCA 4 ‘C1’
ROLL NO.:68

PROBLEM STATEMENT 3: Consider a number entered by a user. Now


calculate the Factorial of this number.

num = int(input("Enter the no: "))

factorial = 1

if num < 0:

print("Sorry, factorial does not exist for negative numbers")

elif num == 0:

print("The factorial of 0 is 1")

else:

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

factorial = factorial*i

print("The factorial of",num,"is",factorial)

Output:

Enter the no: 4

The factorial of 4 is 24
NAME: SHIVAM NEGI
COURSE – BCA 4 ‘C1’
ROLL NO.:68

PROBLEM STATEMENT 4: Write a python program to print the


Fibonacci sequence upto the range entered by a user.

nterms = int(input("How many terms? "))

n1, n2 = 0, 1

count = 0

if nterms <= 0:

print("Please enter a positive integer")

elif nterms == 1:

print("Fibonacci sequence upto",nterms,":")

print(n1)

else:

print("Fibonacci sequence:")

while count < nterms:

print(n1)

nth = n1 + n2

n1 = n2

n2 = nth , count += 1

Output:

How many terms? 6

Fibonacci sequence:
NAME: SHIVAM NEGI
COURSE – BCA 4 ‘C1’
ROLL NO.:68

PROBLEM STATEMENT 5: Write a python program to check whether


the number entered by the user is a Perfect number or not.

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

my_sum = 0

for i in range(1, n):

if(n % i == 0):

my_sum = my_sum + i

if (my_sum == n):

print("The number is a perfect number")

else:

print("The number is not a perfect number")

Output:

Enter the number: 28

The number is a perfect number


NAME: SHIVAM NEGI
COURSE – BCA 4 ‘C1’
ROLL NO.:68

PROBLEM STATEMENT 6: Consider any long string. Now replace each


space between two words with the tab (i.e. the space created
when you press the key 'Tab' from your keyboard).

original_string = "This is a long string with spaces between


words." tab_replaced_string = original_string.replace(" ", "\t")
print(tab_replaced_string)

Output:

This is a long string with spaces between words.


NAME: SHIVAM NEGI
COURSE – BCA 4 ‘C1’
ROLL NO.:68

PROBLEM STATEMENT 7: Take any list and print it in the following


manner:
a) Print only last 3 elements
b) Print all values except first and last value
c) Print only first 3 elements

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

print("a) Last 3 elements:", my_list[-3:])

print("b) All values except first and last:", my_list[1:-1])

print("c) First 3 elements:", my_list[:3])

Output:

a) Last 3 elements: [8, 9, 10]

b) All values except first and last: [2, 3, 4, 5, 6, 7, 8, 9]

c) First 3 elements: [1, 2, 3]


NAME: SHIVAM NEGI
COURSE – BCA 4 ‘C1’
ROLL NO.:68

PROBLEM STATEMENT 8: Python program to remove duplicate


values from a list.

my_list = [1, 2, 2, 3, 4, 4, 5]

unique_list = []

for item in my_list:

if item not in unique_list:

unique_list.append(item)

print(unique_list)

Output:

[1, 2, 3, 4, 5]
NAME: SHIVAM NEGI
COURSE – BCA 4 ‘C1’
ROLL NO.:68

PROBLEM STATEMENT 9: Consider a tuple having values integer,


decimal and string. Write a python program to delete all
decimal values and add 3 characters values in it.

my_tuple = (10, 3.14, "hello", 5.2, "world", 9)

modified_list = []

for item in my_tuple:

if not isinstance(item, float):

modified_list.append(item)

modified_list.extend(['x', 'x', 'x'])

modified_tuple = tuple(modified_list)

print(modified_tuple)

Output:

(10, 'hello', 'world', 9, 'x', 'x', 'x')


NAME: SHIVAM NEGI
COURSE – BCA 4 ‘C1’
ROLL NO.:68

PROBLEM STATEMENT 10: Make a dictionary in which keys are in


numbers and values are cube of that key.
a) Print this dictionary
b) Print only its value
c) Now first empty that dictionary than again print it.

cubes = {}

for num in range(1, 6):

cubes[num] = num**3

print("Dictionary with cubes:", cubes)

print("Values:",cubes.values())

cubes.clear()

print("Dictionary after clearing:", cubes)

Output:

Dictionary with cubes: {1: 1, 2: 8, 3: 27, 4: 64, 5: 125}

Values: dict_values([1, 8, 27, 64, 125])

Dictionary after clearing: {}


NAME: SHIVAM NEGI
COURSE – BCA 4 ‘C1’
ROLL NO.:68

PROBLEM STATEMENT 11: Write a python program having a user


defined function which will calculate the total number of
vowels used in string entered by a user.

def count_vowels(text):

vowel_count = 0

for char in text:

if char == 'a' or char == 'e' or char == 'i' or char == 'o' or char == 'u'
or \ char == 'A' or char == 'E' or char == 'I' or char == 'O' or char ==
'U': vowel_count += 1

return vowel_count

user_text = input("Enter a string: ")

vowel_count = count_vowels(user_text)

print("The number of vowels in the string is:", vowel_count)

Output:

Enter a string: naveen

The number of vowels in the string is: 3


NAME: SHIVAM NEGI
COURSE – BCA 4 ‘C1’
ROLL NO.:68

PROBLEM STATEMENT 12: A shop will give discount of 10% if the cost
of purchased quantity is more than 1000 rupees. Now write a
python program having a user defined function which will first
calculate whether a user purchased quantities more than 1000
rupees or not and then accordingly it will print the total cost for
user.

def calculate_total_cost(price, quantity):

cost = price * quantity

if cost > 1000:

discount = cost * 0.1

cost -= discount

return cost

price = float(input("Enter the price per item (in rupees): "))

quantity = int(input("Enter the number of items purchased: "))

total_cost = calculate_total_cost(price, quantity)

if total_cost == price * quantity:

print("Total cost for", quantity, "items at", price, "rupees per item:",
total_cost, "rupees (no discount applied)")

else:

discount_applied = price * quantity - total_cost


print("Total cost for", quantity, "items at", price, "rupees per item:",
total_cost, "rupees (discount of", discount_applied, "rupees applied)")

Output:

Enter the price per item (in rupees): 800

Enter the number of items purchased: 3

Total cost for 3 items at 800.0 rupees per item: 2160.0 rupees (discount of 240.0
rupees applied)
NAME: SHIVAM NEGI
COURSE – BCA 4 ‘C1’
ROLL NO.:68

PROBLEM STATEMENT 13: Suppose a company decided to give bonus


of 5% to their employee if his/her year of service in the
company is more than 5 years. Now write a python program
having a user defined function which will print the net bonus
amount. Ask user to input the salary and the year of service.

def calculate_bonus(salary, years_of_service):

if years_of_service > 5:

bonus_percentage = 0.05

bonus_amount = salary * bonus_percentage

return bonus_amount

else:

return 0

salary = float(input("Enter the employee's salary (in rupees): "))

years_of_service = int(input("Enter the employee's years of service: "))

bonus_amount = calculate_bonus(salary, years_of_service)

if bonus_amount > 0:

print("The net bonus amount for the employee is:", bonus_amount, "rupees.")

else:

print("The employee is not eligible for a bonus (less than 5 years of service).")
Output:

Enter the employee's salary (in rupees): 25000

Enter the employee's years of service: 6

The net bonus amount for the employee is: 1250.0 rupees.

You might also like