Python Programs
Informatics Practices (065)
=============================
1. WAP in Python
to Find the Square Root of any number
a = float(input('Enter a number: '))
sqrt = a ** 0.5
print('The square root of ',a, 'is',sqrt)
-------------------------------------
2. WAP in Python to Calculate
the Area of a Triangle (Heron’s Method)
a = float(input('Enter first side: '))
b = float(input('Enter second side: '))
c = float(input('Enter third side: '))
# Calculate the semi-perimeter
s = (a + b + c) / 2
# Calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is ', area)
--------------------------------------
3. WAP in Python to Generate a
Random Number
# import the random module
import random
print(random.randint(1,20))
----------------------------------------
4. WAP in Python to Check if a
Number is Even or Odd
num = int(input("Enter a number: "))
if (num % 2) == 0:
print(num, "is
Even")
else:
print(num, "is Odd")
--------------------------------------
5. WAP in Python to Print all
Prime Numbers in an Interval
lower = int(input('Enter the lower number :'))
upper = int(input('Enter the upper number :'))
print("Prime numbers between", lower,
"and", upper, "are:")
for num in range(lower, upper + 1):
# all prime numbers are greater than 1
if num > 1:
for i in
range(2, num):
if (num % i)
== 0:
break
else:
print(num)
-------------------------------------------------
6. WAP to Print the Fibonacci sequence of n-terms
nterms = int(input("How many terms? "))
a1, a2 = 0, 1
count = 0
if nterms <= 0:
print("Please
enter a valid number")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(a1)
else:
print("Fibonacci sequence of", nterms, 'numbers')
while count <
nterms:
print(a1)
nth = a1 + a2
a1 = a2
a2 = nth
count += 1
7. WAP to Find the Factorial of any Number
num = int(input("Enter any number: "))
factorial = 1
if num < 0:
print("Sorry,
this is negative number")
elif num == 0:
print("The
factorial of 0 is 1")
else:
for i in range(1,num
+ 1):
factorial =
factorial*i
print("The
factorial of",num,"is",factorial)
--------------------------------------------
No comments:
Post a Comment
Please do not any spam in the comment box.