Olevel Python Program1
Olevel Python Program1
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 : "))
# 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
Solution:
a = int(input("Enter First Number: "))
b = int(input("Enter Second Number: "))
sum = 0
for i in range(b):
sum = sum + a
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
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
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
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