"""
1. write a program, that given a number as input,
prints its square, cube, fourth power without
using any unnecessary calculations.
"""
num = int(input("Enter a number: "))
print("Square: ", num*num) #square
print("Cube: ", num*num*num) #cube
print("Fourth Power: ", num*num*num*num) #Fourth Power
"""
2. WAP, that reads the dimensions of a rectangle, say length and breadth, finds its area and perimeter. The program should output both the quantities with proper label.
"""
# Read the input from the user
length = float(input("Enter the length of the rectangle in centieters: "))
breadth = float(input("Enter the breadth of the rectangle in centieters: "))
# Compute area and the perimeter
area = length * breadth
perimeter = 2 * (length + breadth)
# Display the result
print(f"Area: {area} sq-cm. \n \bPerimeter: {perimeter} cm.")
"""
3. WAP to calulate area and perimeter of a circle
"""
PI = 3.14159265359
radius = float(input("Enter radius: "))
print(f"Area: {PI*radius*radius}")
print(f"Circumference: {2*PI*radius}")
"""
4. Prime Number or Not
"""
# taking input from user
number = int(input("Enter any number: "))
# prime number is always greater than 1
if number > 1:
for i in range(2, int(number**0.5) + 1):
if (number % i) == 0:
print(number, "is not a prime number")
break
else:
print(number, "is a prime number")
# if the entered number is less than or equal to 1
# then it is not prime number
else:
print(number, "is not a prime number")
"""
5. WAP to read a number 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)
"""
6. WAP to 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)
or
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)
"""
7. WAP to 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)
"""
8. WAP to 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")
"""
9. WAP to 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)
"""
10. WAP to check whether a given 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!")
"""
11. WAP to 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))
"""
12. WAP to read a number n and print and compute the series "1+2+...+n=".
"""
n=int(input("Enter a number: "))
a=[]
for i in range(1,n+1):
print(i,sep=" ",end=" ")
if(i < n):
print("+",sep=" ",end=" ")
a.append(i)
print("=",sum(a))
print()
"""
13. WAP to 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")
"""
14. WAP to find largest and smallest numbers in a list.
"""
lst = []
num = int(input('How many numbers: '))
for n in range(num):
numbers = int(input('Enter number '))
lst.append(numbers)
print("Maximum element in the list is :", max(lst), "\nMinimum element in the list is :", min(lst))
"""
15. WAP to calculate LCM and GCD.
"""
# GCD and LCM are not in math module. They are in gmpy, but these are simple enough:
def main():
print(gcd(40,60))
print(lcm(40,60))
def gcd(a,b):
"""Compute the greatest common divisor of a and b"""
while b > 0:
a, b = b, a % b
return a
def lcm(a, b):
"""Compute the lowest common multiple of a and b"""
return a * b / gcd(a, b)
if __name__=="__main__":main()
"""
16. WAP with a function to calculate x raised to the power y
"""
def power(x, y):
if (y == 0): return 1
elif (int(y % 2) == 0):
return (power(x, int(y / 2)) *
power(x, int(y / 2)))
else:
return (x * power(x, int(y / 2)) *
power(x, int(y / 2)))
# Driver Code
x = 2; y = 3
print(power(x, y))