0% found this document useful (0 votes)
2 views5 pages

Python Practical programs

The document contains a series of Python programming exercises with solutions, including checking if a number is positive, negative, or zero, determining if a number is odd or even, reversing an integer, checking for Armstrong numbers, displaying multiplication tables, calculating factorials, generating Fibonacci sequences, finding the largest of three numbers, checking for prime numbers, and calculating student grades based on marks. Each exercise includes sample input and output to demonstrate functionality. The programs are designed to enhance understanding of basic programming concepts in Python.

Uploaded by

mv3176261
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views5 pages

Python Practical programs

The document contains a series of Python programming exercises with solutions, including checking if a number is positive, negative, or zero, determining if a number is odd or even, reversing an integer, checking for Armstrong numbers, displaying multiplication tables, calculating factorials, generating Fibonacci sequences, finding the largest of three numbers, checking for prime numbers, and calculating student grades based on marks. Each exercise includes sample input and output to demonstrate functionality. The programs are designed to enhance understanding of basic programming concepts in Python.

Uploaded by

mv3176261
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

1.

Python Program to Check if a Number is Positive, Negative or 0


Sol)
num = float(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")

Output:
Enter a number:55
Positive Number

Enter a number:0
Zero

2. Write a Python Program to Check if a Number is Odd or Even?


Sol)
a=int(input(“Enter a number”)
r=a%2
if r==0:
print(a,”is a even number”)
elif r>0:
print(a,”is aodd number”)
else:
print(“You enter a number 0 or less than 0”)

Output:
Enter a number:25
25 is a odd number

3. Write a python program to reverse an integer?


Sol)
n=int(input(“Enter a integer:”)
x=n
r=0
while n>0:
d=n%10
r=r*10+d
n=n//10
Print(“the reversed integer is:”,r)

Output:
Enter the integer:52145666
The reversed integer is:6664125
4. Write a Program to check Armstrong number program in python (For three digits)?
Sol)
num = int(input("Enter a number: "))
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# display the result
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")

Output:
Enter a number: 153
153 is an Armstrong number

5. Write a Python Program to Display the Multiplication Table?

Sol)

numb = int(input(" Enter a number : "))


# using the for loop to generate the multiplication tables
print("Table of: ")
for a in range(1,11):
print(num,'x',a,'=',num*a)

Output:
Enter the number : 7
Multiplication Table of :
7x1=7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

6. Write a Python Program to calculate the factorial of a number?

Sol)

n = int (input (“Enter the number for which the factorial needs to be calculated: “)
factorial = 1
if n < 0:
print (“Factorial cannot be calculated, non-integer input”)
elif n == 0:
print (“Factorial of the number is 1”)
else:
for i in range (1, n+1):
factorial = factorial *i
print (“Factorial of the given number is: “, factorial)

Output:
Enter the number for which the factorial needs to be calculated: 4
Factorial of the given number is: 24

7. Write a python # Program to display the Fibonacci sequence up to n-th term?


Sol)
nterms = int(input("How many terms? "))
# first two terms
n1, n2 = 0, 1
count = 0
# check if the number of terms is valid
if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1

Output:
How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8

8. Write a Python program to find the largest number among the three input numbers
Sol)
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))

if (num1 >= num2) and (num1 >= num3):


largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number is:", largest)

Output:
Enter first number:110.5
Enter second Number:125.5
Enter third number:100
The largest number is:125.5

9. Write a python program to check if a number is prime or not?


Sol)
#num = int(input("Enter a number: "))
flag = False
if num == 1:
print(num, "is not a prime number")
elif num > 1:
# check for factors
for i in range(2, num):
if (num % i) == 0:
# if factor is found, set flag to True
flag = True
# break out of loop
break
# check if flag is True
if flag==True:
print(num, "is not a prime number")
else:
print(num, "is a prime number")

Output:
Enter a number: 15
15 is not a prime number

Enter a number: 17
17 is not a prime number

10. Write a Python Program to input marks of 4 subjects and display Total, Percentage, Result and
Grade. If student is fail (<40) in any subject then Result should be displayed as “FAIL” and Grade
should be displayed as "With Held**"
Sol)
m1=int(input("Enter Mark 1 : "))
m2=int(input("Enter Mark 2 : "))
m3=int(input("Enter Mark 3 : "))
m4=int(input("Enter Mark 4 : "))
total=(m1+m2+m3+m4)
per=total/4
if(m1 < 35 or m2 < 35 or m3 < 35 or m4 < 35):
print("Result : Fail")
print("Grade : With Held**")
else:
print("Result : Pass")
if(per>=90):
print("Grade : A+")
elif(per>=80 and per<90):
print("Grade : A")
elif(per>=70 and per<80):
print("Grade : B+")
elif(per>=60 and per<70):
print("Grade : B")
elif(per>=50 and per<60):
print("Grade : C")
print("Percentage : ",per)

Output:
Enter Mark 1:55
Enter Mark 2:45
Enter Mark 3:65
Enter Mark 4:55

Result: Pass
Grade: C
Percentage: 55

You might also like