0% found this document useful (0 votes)
5 views11 pages

Olevel Python Program1

The document contains a series of Python programming exercises with solutions, including Armstrong numbers, series summation, multiplication through repeated addition, wage calculation for laborers, character replacement in strings, and list manipulations. Each question is followed by a detailed code solution demonstrating various programming concepts. The exercises aim to enhance programming skills and understanding of Python functions and control structures.
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)
5 views11 pages

Olevel Python Program1

The document contains a series of Python programming exercises with solutions, including Armstrong numbers, series summation, multiplication through repeated addition, wage calculation for laborers, character replacement in strings, and list manipulations. Each question is followed by a detailed code solution demonstrating various programming concepts. The exercises aim to enhance programming skills and understanding of Python functions and control structures.
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
You are on page 1/ 11

IT PROFESSIONAL COMPUTER INSTITUTE

Python Programs
_______________________________________
Questions 1. Write a program to print all Armstrong numbers in a given
range.
Note: An Armstrong number is a number whose sum of cubes of digits is
3 3 3
equal to the number itself. E.g. 370=3 +7 +0

Solution:
start_val = int(input("Enter Start Number : "))
end_val = int(input("Enter Stop Number : "))

for num in range(start_val, end_val + 1):

# length of number
power = len(str(num))

# initialize sum
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** power
temp //= 10

if num == sum:
print(num)

Created by – Raj
IT PROFESSIONAL COMPUTER INSTITUTE

Created by – Raj
IT PROFESSIONAL COMPUTER INSTITUTE

Quetions 2. Write a function to obtain sum n terms of the following series for any
positive integer value of X
X +X3 /3! +X5 /5! ! +X7 /7! + …

Solution: Output
method 1
def power(a, b):
p=1
for i in range(1, b + 1):
p=p*a
return p

def factorial(a):
fact = 1
for i in range(1, a + 1):
fact = fact * i
return fact

sum = 0
n = int(input('Enter the number of terms: '))
x = int(input('Enter the value of x: '))
for i in range(1, 2 * n + 1, 2):
sum = sum + power(x, i) / factorial(i)
print(sum)

Method 2
import math
sum = 0
n = int(input('Enter the number of terms: '))
x = int(input('Enter the value of x: '))
for i in range(1, 2 * n + 1, 2):
sum = sum + pow(x, i) / math.factorial(i)
print(sum)

Created by – Raj
IT PROFESSIONAL COMPUTER INSTITUTE

Question 3. Write a function to obtain sum n terms of the following series for any
positive integer value of X
1+x/1!+x2/2!+x3/3!+…

Solution:
import math
sum = 1
n = int(input('Enter the number of terms: '))
x = int(input('Enter the value of x: '))
for i in range(1,n):
sum = sum + pow(x, i) / math.factorial(i)
print(sum)

Output

Created by – Raj
IT PROFESSIONAL COMPUTER INSTITUTE

Question 4. Write a program to multiply two numbers by repeated


addition
e.g. 6*7 = 6+6+6+6+6+6+6

Solution:
a = int(input("Enter First Number: "))
b = int(input("Enter Second Number: "))
sum = 0
for i in range(b):
sum = sum + a

print("The multiply in repeated form is: ",sum)

Output

Created by – Raj
IT PROFESSIONAL COMPUTER INSTITUTE

Question 5. Write a program to compute the wages of a daily laborer as per the
following rules: -
Hours Worked Rate Applicable Upto first 8 hrs Rs100/-
a) For next 4 hrs Rs30/- per hr extra
b) For next 4 hrs Rs40/- per hr extra
c) For next 4 hrs Rs50/- per hr extra
d) For rest Rs60/- per hr extra

Solution
worked_h = int(input("Enter Total Worked hours: "))
if worked_h <= 8:
wages = worked_h*100
elif worked_h > 8 and worked_h <= 12:
wages = (worked_h-8)*30+worked_h*100
elif worked_h > 12 and worked_h <= 16:
wages = (worked_h-8)*40+worked_h*100
elif worked_h > 16 and worked_h <= 20:
wages = (worked_h-8)*50+worked_h*100
else:
wages = (worked_h-8)*60+worked_h*100
print("Total Wages:", wages)

Output

Created by – Raj
IT PROFESSIONAL COMPUTER INSTITUTE

Question 6. Accept the name of the labourer and no. of hours worked.
Calculate and display the wages. The program should run for N number of
labourers as specified by the user.

Solution:
N = int(input("Enter Numbers of Laborers: "))
wages = int(input("Enter Wages per hour:"))
for i in range(N):
name = input("Enter name of laborer :")
hours = int(input("Enter Worked Hours:"))
Total_wages = wages*hours
print("labor name is",name, "and wages",Total_wages)

Output

Created by – Raj
IT PROFESSIONAL COMPUTER INSTITUTE

Question 7. Write a function that takes a string as parameter and returns a


string with every successive repetitive character replaced by? e.g. school may
become scho?l.

Solution
def char_replace(str):
result = ""
for i in str:
if i in result:
result = result + "?"
else:
result = result + i
return result
string = input("Enter a String : ")
print(char_replace(string))

Output

Created by – Raj
IT PROFESSIONAL COMPUTER INSTITUTE

Question 8. Write a program that takes in a sentence as input and displays


the number of words, number of capital letters, no. of small letters and
number of special symbols.

Solution
str = input("Enter a String:")
str = str.strip()
upper = lower = symbols = 0
word = str.count(" ")+1
for c in str:
if c.isupper():
upper += 1
elif c.islower():
lower += 1
elif (not c.isalnum() and not c.isspace()):
symbols += 1
print("Number of Word is ",word)
print("Number of Uppercase letter is ",upper)
print("Number of lowercase letter is ",lower)
print("Number of Special Symbols is ",symbols)

Output

Created by – Raj
IT PROFESSIONAL COMPUTER INSTITUTE

Question 9. Write a Python program that takes list of numbers as input


from the user and produces a cumulative list where each element in the list at
any position n is sum of all elements at positions upto n-1.

Solution
list1 = eval(input("Enter Your List :"))
sum = 0
result = []
for i in list1:
sum = sum + i
result.append(sum)
print("Cumulative list",result)

Output

Created by – Raj
IT PROFESSIONAL COMPUTER INSTITUTE

Question 10. Write a program which takes list of numbers as input and finds:
a) The largest number in the list
b) The smallest number in the list
c) Product of all the items in the list

Solution
list1 = eval(input("Enter Your List:"))
large = max(list1)
small = min(list1)
product = 1
for i in list1:
product = product*i
print("Largest Number in list is ",large)
print("Smallest Number in list is ",small)
print("Product of all Number in list is ",product)

Output

Created by – Raj

You might also like