0% found this document useful (0 votes)
31 views4 pages

Python Programs for Number Analysis

The document contains several Python programming exercises that include checking if a number is positive, negative, or zero; determining if a number is prime and calculating the sum of the first six prime numbers; checking divisibility by 5; calculating the factorial of a number; finding the area of a triangle given its sides; and identifying the largest and smallest numbers in a list. Each exercise includes sample input and output to illustrate the expected functionality of the code. The document serves as a practical guide for basic programming concepts in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views4 pages

Python Programs for Number Analysis

The document contains several Python programming exercises that include checking if a number is positive, negative, or zero; determining if a number is prime and calculating the sum of the first six prime numbers; checking divisibility by 5; calculating the factorial of a number; finding the area of a triangle given its sides; and identifying the largest and smallest numbers in a list. Each exercise includes sample input and output to illustrate the expected functionality of the code. The document serves as a practical guide for basic programming concepts in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

[Link] a program in python that asks for a number and checks if it is positive, negative, or zero.

#Input
num = float(input("Enter a number:"))
if num > 0:
print(num, "is a positive number")
elif num == 0:
print("Zero")
else:
print(num," is a negative number")
#Output

Enter a number:5
5.0 is a positive number

[Link] a program in python to check whether a number is prime or not. Also find the sum of the
first six prime numbers.

#Input
n=int(input("Enter the number: "))
r=0
for i in range(2,n):
a=n%i
if a!=0:
r=r+1
if r==n-2:
print ("This is a prime number")
else:
print ("This is not a prime number")
#Output

Enter the number: 4


This is not a prime number

#Input
n=int(input("Enter the number:"))
for i in range (1,n+1):
x=i
r=0
for j in range(2,int(x)):
a=x%j

if a!=0:
r=r+1

if r==(x-2):
print(x," is a prime number ")
else:
print(x," is not a prime number ")
num=int(input("Enter the number:"))
def prime_numbers(num):
sum=0
for j in range(2,num+1):
for i in range(2,j):
if(j%i==0):
break
else:
print("prime numbers", j)
sum=sum+j
return sum
print("Sum of prime numbers",prime_numbers(num))
#Output

Enter the number:5


1 is not a prime number
2 is a prime number
3 is a prime number
4 is not a prime number
5 is a prime number
Enter the number:15
prime numbers 2
prime numbers 3
prime numbers 5
prime numbers 7
prime numbers 11
prime numbers 13
Sum of prime numbers 41

11. Write a program in python that checks if a given number is divisible by 5.

#Input
num=int(input("please enter any positive integer:"))
a=num%5
if a==0:
print(num,"is divisible by 5")
else:
print(num,"is not divisible by 5")
#Output

please enter any positive integer:656545


656545 is divisible by 5

12. Write a program in python that calculates the factorial of a number.

#Input
n = int (input ("Enter a number: "))
factorial = 1
if n >= 1:
for i in range (1, n+1):
factorial=factorial *i
print("Factorial of the given number is: ", factorial)
#Output

Enter a number: 4
Factorial of the given number is: 24

13. Write a python program in python that asks the user to enter three sides of a triangle and find
the area of the triangle.

#Input
a = float(input('Enter first side: '))
b = float(input('Enter second side: '))
c = float(input('Enter third side: '))
s=(a+b+c)/2
area=((s-a)*(s-b)*(s-c))**0.5
print("Area of the triangle",area)
#Output

Enter first side: 4


Enter second side: 5
Enter third side: 6
Area of the triangle 3.6228441865473595

14. Write a program in python to find the largest and smallest numbers in a list.

#Input
lst = []
num = int(input('How many numbers: '))
for n in range(num):
numbers = int(input('Enter number '))
[Link](numbers)
print("Maximum element in the list is :", max(lst))
print("Minimum element in the list is :", min(lst))
#Output

How many numbers: 4


Enter number 5
Enter number 4
Enter number 3
Enter number 2
Maximum element in the list is : 5
Minimum element in the list is : 2

You might also like