0% found this document useful (0 votes)
10 views21 pages

Python

The document contains a series of Python programming exercises that cover various topics such as basic arithmetic operations, calculating area, percentage, simple interest, checking even/odd numbers, finding the largest number, checking for prime numbers, calculating factorials, generating Fibonacci series, reversing numbers, checking Armstrong numbers, and string manipulations. Each exercise includes a program code snippet, sample output, and user prompts for input. The programs demonstrate fundamental programming concepts and techniques in Python.

Uploaded by

adhvaithadarsh
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)
10 views21 pages

Python

The document contains a series of Python programming exercises that cover various topics such as basic arithmetic operations, calculating area, percentage, simple interest, checking even/odd numbers, finding the largest number, checking for prime numbers, calculating factorials, generating Fibonacci series, reversing numbers, checking Armstrong numbers, and string manipulations. Each exercise includes a program code snippet, sample output, and user prompts for input. The programs demonstrate fundamental programming concepts and techniques in Python.

Uploaded by

adhvaithadarsh
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

Q1 write a program to accept two integers and print their sum in

python

Program:
num1_str = input("Enter the first integer: ")
num1 = int(num1_str)
num2_str = input("Enter the second integer: ")
num2 = int(num2_str)
sum_of_numbers = num1 + num2
print("The sum of the two integers is:", sum_of_numbers)

Output:
Enter the first integer: 3
Enter the second integer: 4
The sum of the two integers is: 7
Q2 Write a Python program that accepts radius of a circle
and prints its area.

Program:
r = float(input("Enter radius of circle: "))
a = 3.14159 * r * r
print("Area of circle =", a)

Output:
Enter radius of circle: 44
Area of circle = 6082.11824
Q3 Write a program to enter marks of 5 subjects and
calculate percentage.

Program:
print("Enter Marks Obtained in 5 Subjects: ")
mOne = int(input())
mTwo = int(input())
mThree = int(input())
mFour = int(input())
mFive = int(input())
sum = mOne+mTwo+mThree+mFour+mFive
perc = (sum/500)*100
print('percentage=',perc)

Output:
Enter Marks Obtained in 5 Subjects:
23
34
65
76
77
percentage= 55.0
Q4 write a program to calculate simple interest.

Program:
print("Enter the Principle Amount: ")
p = int(input())
print("Enter Rate of Interest per month (%): ")
r = float(input())
print("Enter Time Period in months: ")
t = float(input())
si = (p*r*t)/100
print("\nSimple Interest Amount: ")
print(si)
Output:
Enter the Principle Amount:
300000
Enter Rate of Interest per month (%):
12
Enter Time Period in months:
22
Simple Interest Amount:
792000.0
Q5 Write a program to check whether a number is even or
odd.

Program:
x=int(input("enter a number"))
if x % 2 == 0:
print("Even")
else:
print("Odd")

Output:
1)enter a number 33
Odd
2)enter a number 22
Even
Q6 Write a program to find the largest of three numbers.

Program:
a = int(input('enter a number:'))
b = int(input('enter a number:'))
c = int(input('enter a number:'))
res = max(a, b, c)
print(res)

Output:
enter a number:33
enter a number:43
enter a number:55
55
Q7 write a program to input a number and check whether it
is prime or not.

Program:
n=int(input('Enter a number:'))
if n <= 1:
print(False)
else:
is_prime = True # Flag variable
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
is_prime = False
break
print(is_prime)

Output:
Enter a number:21
False
Enter a number:17
True
Q8 write a program to find the factorial of a number.

Program:
num=int(input('Enter a number:'))
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 a number:7
The factorial of 7 is 5040
Q9 write a program to print Fibonacci series first 20
elements.

Program:
n = 20
a=0
b=1
next = a
count = 1
while count <= n:
print(next, end=" ")
count += 1
a, b = b, next
next = a + b
print()

Output:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
Q10 write a program to read an integer greater than 1000
and reverse.

Program:
num = int(input('Enter a number greater than 1000:'))
reversed_num = 0
while num != 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10
print("Reversed Number: " + str(reversed_num))

Output:
Enter a number greater than 1000:1234
Reversed Number: 4321

Q11 write a program to check is an input number is


Armstrong or not.
Program:
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: 407
407 is an Armstrong number

Q12 Generate the following:


1)Half pyramid using * 2) Inverted half pyramid using numbers
3)Half pyramid using alphabets
1)Program:
rows = 5
for i in range(rows):
for j in range(i+1):
print("* ", end="")
print()

2)Program:
rows = 5
for i in range(rows, 0, -1):
for j in range(1, i+1):
print(j, end=" ")
print()

3)Program:
rows =5
ascii_value = 65
for i in range(rows):
for j in range(i+1):
alphabet = chr(ascii_value+j)
print(alphabet, end=" ")
print()

Output:
1)
*
**
***
****
*****
2)
12345
1234
123
12
1

3)
A
AB
ABC
ABCD
ABCDE

Q13 Write a program to input the values of x and n then


print the sum of the following series:
1. 1+x+x2+x3+………+xn
2. 1-x+x2-x3+…….xn
3. x+x2/2+x3/3+……. +xn/n
4. x+x2/2! +x3/3! +……. +xn/n!
Program:
1.
x=int(input('enter base number:'))
n=int(input('enter power:'))
s=1
for i in range(1,n+1):
s=s+x**i
print(s)
2.
x=int(input('enter base number:'))
n=int(input('enter power:'))
s=1
for i in range(1,n+1):
s=s+x**i*(-1)**i
print(s)

3.
x = int(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))
sum = 0
for i in range(1, n + 1) :
term = x ** i / i
sum += term
print("Sum =", sum)
4.
x=int(input('enter base number:'))
n=int(input('enter power:'))
def f(n):
if n==0:
return 1
else:
f=1
for i in range(1,n+1):
f=f*i
return f
s=0
for i in range(1,n+1):
s=s+(x**i)/f(i)
print(s)

Output:
1.
enter base number:2
enter power:3
15
2.
enter base number:2
enter power:2
3
3.
Enter the value of x: 2
Enter the value of n: 3
Sum = 6.666666666666666
4.
enter base number:2
enter power:3
5.333333333333333
Q14 Write a program to count and display the number of vowels,
consonants, uppercase and lowercase letters in an inputted string.
Program:
def analyze_string(input_string):

vowels = "aeiouAEIOU"

vowel_count = 0

consonant_count = 0

uppercase_count = 0

lowercase_count = 0

for char in input_string:

if [Link]():

if char in vowels:

vowel_count += 1

else:

consonant_count += 1

if [Link]():

uppercase_count += 1

elif [Link]():

lowercase_count += 1

print(f"Number of vowels: {vowel_count}")

print(f"Number of consonants: {consonant_count}")

print(f"Number of uppercase letters: {uppercase_count}")

print(f"Number of lowercase letters: {lowercase_count}")

user_input = input("Enter a string: ")

analyze_string(user_input)

Output:
Enter a string: Hello World
Number of vowels: 3
Number of consonants: 8
Number of uppercase letters: 2
Number of lowercase letters: 9
Q15 Write a Program to Check if a String is Palindrome or
Not, convert the case of characters in string.
Program:
s = input('enter a word: ')
i,j = 0, len(s) - 1
is_palindrome = True
while i < j:
if s[i] != s[j]:
is_palindrome = False
break
i += 1
j -= 1
if is_palindrome:
print("Yes")
else:
print("No")
print('the string after case swapped:',[Link]())

Output:
enter a word: malayalam
Yes
the string after case swapped: MALAYALAM
Q16 Write a python program that takes a string with
multiple words and capitalize the first letter of each word
and make a new string out of it.

Program:
s = input('enter a string with multiple words: ')
a=[Link]()
print(a)

Output:
enter a string with multiple words: Hi there
Hi There
Q17 Write a python program that reads a string and display
the number of occurrences of a given substring in it

Program:
s = input('enter a string with multiple words: ')
a = input('enter the string to be counted:')
count=[Link](a)
print('the number of times given substring occurred',count)

Output:
enter a string with multiple words: hi is hi hi is hi
enter the string to be counted: hi
the number of times given substring occurred 4

You might also like