Seven Programs Related To Python
1.Program
to swap the value of a variable entered?
a=int(input("Enter value of first
variable: "))
b=int(input("Enter value of second
variable: "))
a=a+b
b=a-b
a=a-b
print("a is:",a," b
is:",b)
Enter value of second variable: 2
a is: 2
b is: 8
2.Program
to reverse the number entered?
n=int(input("Enter number: "))
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
print("Reverse of the
number:",rev)
Reverse of the number: 102412
3.Program
to find the grade of a student?
sub1=int(input("Enter marks of the
first subject: "))
sub2=int(input("Enter marks of the
second subject: "))
sub3=int(input("Enter marks of the
third subject: "))
sub4=int(input("Enter marks of the
fourth subject: "))
sub5=int(input("Enter marks of the
fifth subject: "))
avg=(sub1+sub2+sub3+sub4+sub4)/5
if(avg>=90):
print("Grade: A")
elif(avg>=80&avg<90):
print("Grade: B")
elif(avg>=70&avg<80):
print("Grade: C")
elif(avg>=60&avg<70):
print("Grade: D")
else:
print("Grade: F")
Enter marks of the second subject: 90
Enter marks of the third subject: 89
Enter marks of the fourth subject: 98
Enter marks of the fifth subject: 86
Grade: A
4.Program
to display matrix?
n=int(input("Enter a number:
"))
for i in range(0,n):
for j in range(0,n):
if(i==j):
print("1",sep="
",end=" ")
else:
print("0",sep="
",end=" ")
print()
1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1
5.Program
to check the entered year is leap year or not?
year=int(input("Enter year to be
checked:"))
if(year%4==0 and year%100!=0 or
year%400==0):
print("The year is a leap year!")
else:
print("The year isn't a leap year!")
The year isn't a leap year!
6.Program
to check the number entered is palindrome or not?
n=int(input("Enter number:"))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")
The number isn't a palindrome!
7.Program
for Pythagorean triplets?
limit=int(input("Enter upper
limit:"))
c=0
m=2
while(c<limit):
for n in range(1,m+1):
a=m*m-n*n
b=2*m*n
c=m*m+n*n
if(c>limit):
break
if(a==0 or b==0 or c==0):
break
print(a,b,c)
m=m+1
3 4 5
8 6 10
5 12 13
15 8 17
12 16 20
7 24 25
24 10 26
21 20 29
16 30 34
9 40 41
35 12 37
32 24 40
27 36 45
20 48 52
11 60 61
48 14 50
45 28 53
40 42 58
33 56 65
24 70 74
-Thank
you
This comment has been removed by the author.
ReplyDelete