0% found this document useful (0 votes)
2 views9 pages

program exercise_2_final

The document contains a series of programming exercises primarily in Python, focusing on basic operations such as addition, interest calculation, number properties (even, odd, prime, perfect), and various patterns. Each exercise includes a brief description followed by the corresponding Python code. The exercises cover a range of topics from simple arithmetic to more complex concepts like Fibonacci series and automorphic numbers.

Uploaded by

m.4nadeem.414
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)
2 views9 pages

program exercise_2_final

The document contains a series of programming exercises primarily in Python, focusing on basic operations such as addition, interest calculation, number properties (even, odd, prime, perfect), and various patterns. Each exercise includes a brief description followed by the corresponding Python code. The exercises cover a range of topics from simple arithmetic to more complex concepts like Fibonacci series and automorphic numbers.

Uploaded by

m.4nadeem.414
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

Q1 WAP to enter 2 number and add them.

Q2 WAP to input principal rate and time. Calculate and print simple interest.

Q3 Write a program to input a no calculate and print sum of first and last digit

Q4 WAP to enter 2 integer number and concat(join) them.

Eg a =23 b=625

Output 23625

Q5 WAP to input 2 no and swap them.

Q1 Write a Python program to input a number and check whether it is


even or odd.

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


if num % 2 == 0:
print(num, "is an even number.")
else:
print(num, "is an odd number.")

Q2 Write a Python program to input three numbers and print the greatest
among them.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

if a >= b and a >= c:


print("Greatest number =", a)
elif b >= a and b >= c:
print("Greatest number =", b)
else:
print("Greatest number =", c)

Q3 Write a Python program to input a year and check whether it is a


leap year or not.
year = int(input("Enter a year: "))
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
print(year, "is a leap year.")
else:
print(year, "is not a leap year.")

Q4 Write a program which calculates electricity billing rates with given charges, scaling surcharges,
and a mandatory 5% penalty fine for delayed payments beyond the due date.

Units Consumed Charge Per Unit Surcharge (₹)

Less than 50 ₹5.50 50

From 50 units to 100 units ₹7.00 75

Above 100 units to 200 units ₹8.00 100

Above 200 units ₹9.00 150

units = int(input("Enter number units consumed: "))


days = int(input("Enter number of late days: "))
fine = 0
if units < 50:
amount = units * 5.50
surcharge = 50
elif units >50 and units <= 100:
amount = 50 * 5.50 + (units - 50) * 7
surcharge = 75
elif units > 100 and units <= 200:
amount = 50 * 5.50 + 50 * 7 +(units - 100) * 8
surcharge = 100
else:
amount = 50* 5.50 + 50* 7 +8*100+(units - 200) * 9
surcharge = 150

total = amount + surcharge


if days > 0:
fine = total * 0.05
total = total + fine

print("\nElectricity Bill = {:.2f} for units{}".format(total,units))

Q WAP to check 'Buzz Number' by analysing if it is divisible by 7 or finishes


with a trailing 7 digit.

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


if n % 7 == 0 or n % 10 == 7:
print(n, "is a Buzz number.")
else:
print(n, "is not a Buzz number.")
WAP to input no and print its Table

n=int(input( "Enter num"))


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

WAP to input print no from 1 to 10 with step value 3


for i in range(1,11,3):
print(i)
output
1
4
7
10

WAP to input no and print its factorial


n=int(input( "Enter num"))
f=1
for i in range(1,n+1):
f=f*i
print("Factorial of ",n,"=",f)

WAP to input no and check whether it is prime number or not

n=int(input("Enter num"))
c=0
for i in range(1,n+1):
if n%i==0:
c=c+1
if c==2:
print("prime")

else:
print("Not prime")

WAP to input no and check whether it is perfect number or not

n=int(input("Enter num"))
s=0
for i in range(1,n):
if n%i==0:
s=s+i
if s==n:
print(n," is perfect num")
else:
print(n," is not perfect num")
WAP to input 2 numbers calculate and print their hcf and lcm

a=int(input("Enter first num "))


b=int(input("Enter second num "))
h=0
l=0
for i in range(1,a+1):
if a%i==0 and b%i==0:
h=i
l=(a*b)/h
print(" HCF of {} and {} is {}".format(a,b,h))
print(" LCM of {} and {} is {}".format(a,b,l))

WAP to input number and print sum of its digits

n=int(input("enter no"))
s=0
while n>0:
r=n%10
s=s+r
n=n//10
print("sum of digits ",s)

WAP to input a number and check whether it is palindrome or not

n=int(input("enter no"))
s=0
t=n
while n>0:
r=n%10
s=s*10+r
n=n//10
if s==t:
print(t, "is palindrome ")
else:
print(t, "is not palindrome ")
WAP to input a no calculate and print smallest digit

n=int(input("enter no"))
min=n%10;
while n>0:
r=n%10
if r<min:
min=r
n=n//10
print( "min num= ",min)

WAP to input a number and check whether it is Armstrong or not

a=input("enter number")
digits=len(a)
n=int(a)
t=n
s=0
while n>0:
r=n%10
s=s+r**digits
n=n//10
if(s==t):
print("Armstrong")
else:
print("Not Armstrong")

WAP to input a number and check whether it is automorphic or not

a1=input("enter number")
a=int(a1)
b=a*a
digits=len(a1)
t=b%(10**digits)
print(a," ",b," ",t)
if a==t:
print("Automorphic")
else:
print("Not Automorphic")
WAP to input a number n eg 5 and print following pattern
enter number5
1
12
123
1234
12345

n=int(input("enter number"))
for i in range(1,n+1):
for j in range(1,i+1):
print(j,end=' ')
print()

WAP to input a number n eg 5 and print following pattern

enter number5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

n=int(input("enter number"))
k=1
for i in range(1,n+1):
for j in range(1,i+1):
print(k,end='\t')
k=k+1
print()

WAP to input a number n eg 5 and print following pattern

enter number5
54321
5432
543
54
5

n=int(input("enter number"))
for i in range(1,n+1):
for j in range(n,i-1,-1):
print(j,end=' ')
print()
enter number4
1234
123
12
1

n=int(input("enter number"))
for i in range(n,0,-1):
for j in range(1,i+1):
print(j,end=' ')
print()

WAP to input 2 no and print all prime nos from a to b

Enter 1 num10
Enter 2 num20
11
13
17
19

a=int(input("Enter 1 num"))
b=int(input("Enter 2 num"))
for i in range(a,b+1):
c=0
for j in range(1,i+i):
if i%j==0:
c=c+1
if c==2:
print(i)
-----------------------------------
enter number4
1234
234
34
4

n=int(input("enter number"))
for i in range(1,n+1):
for j in range(i,n+1):
print(j,end=' ')
print()

enter number4
1
12
123
1234

n=int(input("enter number"))
for i in range(1,n+1):
for j in range(i,n):
print(end=' ')
for k in range(1,i+1):
print(k,end='')
print()

WAP to generate Fibonacci SERIES

n=int(input("Enter num "))


a=0
b=1
print(a,end='')
for i in range(1,n+1):
c=a+b
print(c ,end='')
a=b
b=c

You might also like