PRACTICAL python
PRACTICAL python
• CODE:
• num1 = int(input("Enter the first number: "))
• num2 = int(input("Enter the second number: "))
• sum = num1 + num2
• product = num1 * num2
• print("sum of the number is:-", sum)
• print("product of the number is:-", product)
• OUTPUT:
AIM:2. Write a Program to compute x n
of given two integers x and n. x and n
to be taken using input command.
• CODE: x=int(input("enter the value of x:"))
n=int(input("enter the value of n:"))
# Compute x raised to the power of n using the operator
result = x **n
print(result)
• OUTPUT:
AIM:3. Write a Program to convert the
time taken from the user in minutes
into hours and remaining minutes
• CODE:
• t=int(input("time(in minutes) = "))
• hours = t/60
• minutes t%60
• print(hours, "hours", minutes, "minutes")
• OUTPUT:
AIM:4. Write a Program for calculating simple
interest. All numerical values required to be
taken using input command.
CODE: p=int(input("enter the princple amount-" ))
r=int(input("enter the rate of intrest-"))
t=int(input("enter the rate of intrest-"))
si=p*r*t/100
ta=si+p
print("simple intrest is",si)
print("total amount is",ta)
AIM:5. Write a Program to accept a number
from the user and display whether it is an
even number or odd number.
• CODE:
• n= int(input("enter a number:-"))
• if(n % 2)==0:
• print("even number")
• else:
• print("odd number")
• output
AIM:6. Write a Program to accept percentage
of a student and display its grade accordingly
(using if , elif , else).
• CODE:
• marks = int(input("enter marks:-"))
• if(marks>=75):
• print("a grade")
• elif(marks>=60):
• print("b grade ")
• elif(marks>=35):
• print("c grade")
• else:
• print("fail")
• Output:
AIM:7. Write a Program to print Fibonacci
series up to certain limit (using – while).
Limit to be taken from the user.
• CODE:
• n=int(input("enter the number of terms:-"))
• a=0
• b=1
• i=0
• while i<n:
• print(a)
• c=a+b
• a=b
• b=c
• i+=1
• Output:
AIM:8. Write a Program to display
prime numbers up to a certain limit.
Limit to be taken from the user.
• CODE:
• lower = int(input("lower limit(<1):"))
• upper = int(input("upper limit:"))
• for num in range(lower,upper +1):
• if num > 1:
• for i in range(2, num):
• if (num % i) ==0:
• break
• else:
• print(num)
• Output:
AIM:9. Write a program to calculate and
display Area and Perimeter of a
rectangle. Take floating input from user.
• CODE:
• lenght = float(input("enter the lenght of rectangle:-"))
• brenth = float(input("enter the brenth of rectangle:-"))
• area = lenght*brenth
• print("area of rectangle is", area)
• perimeter = 2*(lenght+brenth)
• print("perimeter of rectangle is",perimeter)
• Output:
AIM:10. Write a Program to swap two
numbers using a third variable.
• CODE:
• a = int (input("enter the first number:-"))
• b = int(input("enter the second number:-"))
• print("before the swapping",a,b)
• d=a,b=b,a
• print("after the swapping",d)
• Output:
AIM:11. Write a Program to swap two
numbers without using a third
variable.
• Code:
• a = int (input("enter the first number:-"))
• b = int(input("enter the second number:-"))
• print("before the swapping",a,b)
• d=a,b=b,a
• print("after the swapping",d)
• Output:
AIM:12. Write a Program to find the
square root of a number.
• Code:
x=int(input("enter the value of x:-"))
root = x**0.5
• print(root)
• Output:
AIM:13. Write a program to check the
largest among the given three numbers
• CODE:
• a = float (input("enter the first number:-"))
• b = float (input("enter the second number:-"))
• c = float (input("enter the third number:-"))
• if (a>b) and (a>c):
• print(a)
• elif (b>a) and (b>c):
• print(b)
• else:
• print(c)
OUTPUT:
Aim:14.Write a Python program to check
if the input year is a leap year or not.
• Code:
year = int(input("inter the year:-"))
if (year%4==0):
print(year,"is a leap year")
else:
print(year,"is not a leap year")
• Output:
AIM:15. Write a program to print from 1
to 10
• CODE:
for i in range (1,11):
print(i)
•
output
AIM:16. Write a program to
print from 1 to n.
• CODE:
n = int(input("enter a number:-"))
for i in range (1,n+1):
print(i)
• Output:
• AIM:17. Write a program to print from
10 to 1.
• Code:for i in range (10,0,-1):
• print(i)
• Output:
AIM:18. Write a program to print
sum of n to 1
CODE:
n = int(input("enter a number:-"))
sum = sum(range(n,0,-1))
print("sum from",n,"to 1 is",sum)
Output:
AIM:19. Write a program to print sum
from 1 to n.
• Code:
• n =int(input("enter a number:-"))
• sum = n*(n+1)/2
• print(sum)
• Output:.
• AIM:20. Write a program to print
sum of SQUARE from 1 to n
• CODE:.
n =int(input("enter a number:-"))
square = sum(i**2 for i in range(1,n+1))
print("sum of square from 1 to n",n,"is:",square)
• Output:.
AIM:21. Write a program to print sum of
CUBE from 1 to n.
• CODE:.
• n =int(input("enter a number:-"))
• cube = sum(i**3 for i in range(1,n+1))
• print("sum of cube from 1 to n",n,"is:",cube)
• Output:.
AIM:22. Write a program to print only
even number between 1 to n
• CODE:.
• n =int(input("enter a number:-"))
• for i in range(1,n+1):
• if i%2==0:
• print(i)
• Output:.
AIM:23. Write a program to find sum of
even numbers from 1 to n
• CODE:. n =int(input("enter a number:-"))
• sum=0
• for i in range(1,n+1):
• if i%2==0:
• sum +=i
• print("sum of evan number from 1 to",n,"is:",sum)
• Output:.