Programming
Q)Find the Area of a Rectangle Using Classes
class rectangle():
def __init__(self,breadth,length):
self.breadth=breadth
self.length=length
def area(self):
return self.breadth*self.length
a=int(input("Enter length of rectangle: "))
b=int(input("Enter breadth of rectangle: "))
obj=rectangle(a,b)
print("Area of rectangle:",obj.area())
print()
Output
Enter length of rectangle: 4
Enter breadth of rectangle: 5
Area of rectangle: 20
Q)Exchange the Values of Two Numbers Without Using a Temporary Variable
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)
Output
Enter value of first variable: 3
Enter value of second variable: 5
a is: 5 b is: 3
Q) Read a Mumber n and Compute n+nn+nnn
n=int(input("Enter a number n: "))
temp=str(n)
t1=temp+temp
t2=temp+temp+temp
comp=n+int(t1)+int(t2)
print("The value is:",comp)
Output
Enter a number n: 5
The value is: 615
Q) Reverse a Given Number
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)
Output
Enter number: 124
Reverse of the number: 421
Q) Check Whether a Number is Positive or Negative
n=int(input("Enter number: "))
if(n>0):
print("Number is positive")
else:
print("Number is negative")
Output
Enter number: 45
Number is positive
Q) Take in the Marks of 5 Subjects and Display the Grade
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")
Output
Enter marks of the first subject: 85
Enter marks of the second subject: 95
Enter marks of the third subject: 99
Enter marks of the fourth subject: 93
Enter marks of the fifth subject: 100
Q) Calculate the Average of Numbers in a Given List
n=int(input("Enter the number of elements to be inserted: "))
a=[]
for i in range(0,n):
elem=int(input("Enter element: "))
a.append(elem)
avg=sum(a)/n
print("Average of elements in the list",round(avg,2))
Output
Enter the number of elements to be inserted: 3
Enter element: 23
Enter element: 45
Enter element: 56
Average of elements in the list 41.33
Q) Print all Numbers in a Range Divisible by a Given Number
lower=int(input("Enter lower range limit:"))
upper=int(input("Enter upper range limit:"))
n=int(input("Enter the number to be divided by:"))
for i in range(lower,upper+1):
if(i%n==0):
print(i)
Output
Enter lower range limit:1
Enter upper range limit:50
Enter the number to be divided by:5
5
10
15
20
25
30
35
40
45
50
Q) Read Two Numbers and Print Their Quotient and Remainder
a=int(input("Enter the first number: "))
b=int(input("Enter the second number: "))
quotient=a//b
remainder=a%b
print("Quotient is:",quotient)
print("Remainder is:",remainder)
Output
Enter the first number: 15
Enter the second number: 7
Quotient is: 2
Remainder is: 1
Q) Accept Three Digits and Print all Possible Combinations from the Digits
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
c=int(input("Enter third number:"))
d=[]
d.append(a)
d.append(b)
d.append(c)
for i in range(0,3):
for j in range(0,3):
for k in range(0,3):
if(i!=j&j!=k&k!=i):
print(d[i],d[j],d[k])
Output
Enter first number:1
Enter second number:2
Enter third number:3
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
Q) Print Odd Numbers Within a Given Range
lower=int(input("Enter the lower limit for the range:"))
upper=int(input("Enter the upper limit for the range:"))
for i in range(lower,upper+1):
if(i%2!=0):
print(i)
Output
Enter the lower limit for the range:1
Enter the upper limit for the range:16
1
3
5
7
9
11
13
15
Q)Find the Sum of Digits in a Number
n=int(input("Enter a number:"))
tot=0
while(n>0):
dig=n%10
tot=tot+dig
n=n//10
print("The total sum of digits is:",tot)
Output
Enter a number:1892
The total sum of digits is: 20
Q)Find the Sum of Digits in a Number
n=int(input("Enter a number:"))
tot=0
while(n>0):
dig=n%10
tot=tot+dig
n=n//10
print("The total sum of digits is:",tot)
Output
Enter a number:1892
The total sum of digits is: 20
Q)Check if a Number is a Palindrome
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!")
Output
Enter number:121
The number is a palindrome!
Q)Print all Integers that Aren't Divisible by Either 2 or 3 and Lie between 1 and 50.
for i in range(0,51):
if(i%2!=0&i%3!=0):
print(i)
Output
1
5
7
11
13
17
19
23
25
29
31
35
37
41
43
47
49
No comments:
Post a Comment