KEMBAR78
Program | PDF
0% found this document useful (0 votes)
6 views51 pages

Program

The document contains a series of programming exercises, each demonstrating a specific task in Python, such as printing 'Hello World', performing arithmetic operations, checking for even or odd numbers, and implementing algorithms like Fibonacci and factorial. Each task is accompanied by code snippets and expected outputs. The exercises cover basic programming concepts, control structures, and string manipulations.

Uploaded by

Vasu vasu
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)
6 views51 pages

Program

The document contains a series of programming exercises, each demonstrating a specific task in Python, such as printing 'Hello World', performing arithmetic operations, checking for even or odd numbers, and implementing algorithms like Fibonacci and factorial. Each task is accompanied by code snippets and expected outputs. The exercises cover basic programming concepts, control structures, and string manipulations.

Uploaded by

Vasu vasu
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
You are on page 1/ 51

Q1) WAP to print “Hello World”

Code: print("Hello World")

Output:
Q2) WAP to add two no.’s

Code: a=int(input("enter 1st no."))

b=int(input("enter 2nd no."))

c=a+b

print(c)

Output:
Q3) WAP to print area of rectangle

Code: a=int(input("enter length of the rectangle"))

b=int(input("enter breadth of the rectangle"))

c=a*b

print("the area of rectangle is:",c)

output:
Q4) WAP to print simple interest

Code: p=float(input("enter principal"))

r=float(input("enter rate"))

t=float(input("enter time"))

SI=p*r*t

print("simple intrest is :",SI)

Output:
Q5) WAP to swap 2 no’s using 2 variables

Code: a=int(input("enter a number"))

b=int(input("enter other number"))

a=a+b

b=a-b

a=a-b

print("after swaping,a=",a,"and b=",b)

Output:
Q6) WAP to add the digits of a 2-digit no.

Code:

a=int(input("Enter a 2 digit number: "))


b=a%10
c=a-b
d=c/10
e=b+d
print("sum of digits of ",a,"is",e)

output:
Q7) WAP to reverse a 2-digit no.

Code: a=int(input("enter a number"))

b=int(input("enter other number"))

c=a

a=b

b=c

print("after swapping")

print("a=",a)

print("b=",b)

Output:
Q8) WAP to check if the no. is odd or even

Code: a=int(input("enter a number"))

if a%2==0:

print("the number is even")

else:

print("the number is odd")

Output:
Q9) WAP to check whether a person is eligible to vote.

Code: a=int(input("enter your age"))

if a>=18:

print("you are eligible for vote")

else:

print("you are not eligible for vote")

Output:
Q10) WAP to check greatest among 2 no’s

Code: a=int(input("enter a number"))

b=int(input("enter other number"))

if a>b:

print(a," is greater than ",b)

elif a<b:

print(b," is greater than ",a)

else:

print("both are equal")

Output:
Q11) WAP to check whether the entered year is leap year.

Code: a=int(input("Enter the current year"))

if a%4==0:

print("this year is leap year")

else:

print("this year is not a leap year")

Output:
Q12) WAP to check whether the angles form a triangle.

Code:

a=float(input("enter 1st angle :"))


b=float(input("enter 2nd angle :"))
c=float(input("enter 3rd angle :"))
if a+b+c==180:
print("its a triangle")
else:
print("it is not a triangle")

Oputput:
Q13) WAP to take marks from the user and display the percentage and grade

Code:

a=int(input("enter marks of 1st subject"))


b=int(input("enter marks of 2nd subject"))
c=int(input("enter marks of 3rd subject"))
d=int(input("enter marks of 4th subject"))
e=int(input("enter marks of 5th subject"))
f=(a+b+c+d+e)/5
print("percentage:",f)
if f>=91 and f<=100:
print("Grade : A")
elif f>=81 and f<=90:
print("Grade : B")
elif f>=71 and f<=80:
print("Grade : C")
elif f<=70:
print("Grade : D")
else:
print("enter correct marks")

Output:
Q14) Write a menu driven program to calculate area of 3 different shapes depending on user’s
choice.

Code:

print("1) area of square")


print("2) area of rectangle")
print("3) area of triangle")
a=int(input("enter your choice"))
if a==1:
s=float(input("enter side of square"))
ar=s**2
print("area of the square is :",ar)
elif a==2:
l=float(input("enter length of rectangle"))
b=float(input("enter breadth of rectangle"))
ar=l*b
print("area of rectangle is :",ar)
elif a==3:
b=float(input("enter breadth of rectangle"))
h=float(input("enter hight of rectangle"))
ar=b*h/2
print("area of triangle is :",ar)
else:
print("invalid choice")

Output:
Q15) Write a program to arrange 3 numbers in ascending order.

Code:
a=int(input("enter 1st number"))
b=int(input("enter 2nd number"))
c=int(input("enter 3rd number"))
if a>b and a>c:
if b>c:
print(c,b,a)
else:
print(b,c,a)
elif b>a and b>c:
if a>c:
print(c,a,b)
else:
print(a,c,b)
elif c>a and c>b:
if a>b:
print(b,a,c)
else:
print(a,b,c)
else:
print("all numbers are equal")

Output:
Q16) WAP to print first 10 natural no.

Code:

for i in range(1,11):
print(i)

Output:
Q17) WAP to print table of a no.

Code:

a=int(input("enter a number"))
for i in range (1,11):
b=a*i
print(b)

Output:
Q18) WAP to print the sum of first n natural no.’s

Code:

n=int(input("enter ""n"""))
s=0
for i in range (1,n+1):
s=s+i
print(s)

output:
Q19) WAP to find the sum of the specified series

. i) x+x2+x3+…+xn

Code:

x=int(input("enter x"))
n=int(input("enter n"))
s=0
for i in range(1,n+1):
s=s+x**i
print(s)

output:
Q19) WAP to find the sum of the specified series.

ii) x+x2 /2+x3 /3+…+xn /n

code:

x=int(input("enter x"))
n=int(input("enter n"))
s=0
for i in range(1,n+1):
s=s+x**i/i
print(s)

output:
Q19) WAP to find the sum of the specified series.

iii) x+x2 /2!+x3 /3!+…+xn /n!

code:

x=int(input("enter x"))
n=int(input("enter n"))
s=0
f=1
for i in range(1,n+1):
f=f*i
s=s+x**i/f
print(s)

Output:

Q19) WAP to find the sum of the specified series.


iv) x-x 2 /2+x3 /3-…+xn /n

code:

x=int(input("enter x"))
n=int(input("enter n"))
s=0
for i in range(1,n+1):
s=s+x**i/i
print(s-x)

Output:

Q20) WAP to print the Fibonacci series.


Code:

a=0
b=1
num=int(input("enter a number"))
print(a)
print(b)
for i in range(2,num):
c=a+b
a=b
b=c
print(c)

Output:

Q21) WAP to calculate factorial of a no


Code:

n=int(input("enter a number "))


f=1
for i in range(1,n+1):
f=f*i
print(f)

output:

Q22) WAP to find sum of digits of a no.


Code:

a=int(input("enter a number"))
s=0
while a>0:
b=a%10
s=s+b
a=a//10
print(s)

Output:

Q23) WAP to find the reverse a no


Code:

a=int(input("enter a number"))
s=0
while a>0:
b=a%10
s=s*10+b
a=a//10
print(s)

Output:

Q24) WAP to check whether the number is Palindrome or not.


Code:

a=int(input("enter a number: "))


b=a%100
c=b%10
d=a-c*100
if d==b:
print("the number is palindrome")
else:
print("the number is not palindrome")

output:

Q25) WAP to check whether the number is Armstrong or not.


Code:

a=int(input("enter a number: "))


b=a%100
c=b%10
d=b//10
e=(a-b)/100
f=e**3
g=d**3
h=c**3
i=f+g+h
if i==a:
print("the number is armstrong")
else:
print("the number is not armstrong")

output:

Q26) WAP check whether a number is perfect or not.


Code:

n=int(input("enter a number"))
s=0
for i in range(1,n):
if n%1==0:
s+=1
if s==n:
print("perfect number")
else:
print("not a perfect number")

Output:

Q27) WAP to take n numbers from the user until he enters a negative number, print the sum and
average of entered positive numbers.
Code:

s=0
while True:
a=int(input("enter a number"))
if a<0:
break
else:
s=s+a
print(s)

Output:

Q28) WAP to print the specified patterns using nested looping.


1)*
**
***
****
Code:

for i in range (1,5):


for j in range (1,i+1):
print("*",end='')
print('/n')

Output:

2) 1

12
123

1234

Code:
for i in range(1,5):
for j in range (1,i+1):
print(j,end='')
print('/n')

Output:

4) *

**
***

****

***

**

Code:

for i in range (1,5):


for j in range (1,i+1):
print('*',end='')
print('\n')
for i in range (4,0,-1):
for j in range(1,i):
print('*',end='')
print('\n')

output:

Q29)WAP to take a string from the user and count the number of upper cases and lower cases
Code:

a=input("enter any string")

b=0

c=0

for i in a:

if i.isupper():

b+=1

elif i.islower():

c+=1

print("total no. of uppercases are", b)

print("total no. of lowercases are",c)

Output:

Q30) WAP to take a string from the user and swap all the cases i.e., upper case to lower case
and vice versa

Code:
a=input("enter a string")
s=''
for i in a:
if i.isupper():

s=s+i.lower()
elif i.islower():
s=s+i.upper()
else:
print("enter only alphabets")
print(s)

Output:

Q31) WAP to take a string from the user and display the sum of digits present in the string

Code:
a = input("Enter a string: ")
s= 0
for i in a:
if i.isdigit():
s+=int(i)
print("The sum of digits in the string is:", s)

Output:

Q32) WAP to take a string from the user and check whether it is palindrome or not
Code:

Q33)

You might also like