Python program| Exercises of python program

1.   Write a python program to print your address.
2.   Write a program to store two integer values in two variables and using them calculate addition, subtraction, multiplication division.
3.   Write a program to print pay-slip using the following data.
Employee number is 100
Basic salary is 5000
DA must be calculated as 10% of basic
HRA must be calculated as 20% of basic
Total salary must be calculated as Basic+DA+HRA
 
4.   Write a program to calculate total marks and percentage of a student of 5 subjects and based on the percentage display the grade according to the following criteria.

 

Below 30%          D
30-45                   C
45-55                   B
55-65                   B+
65-85                   A
Above 85%         A+
 
5.   A company decided to give bonus to employees according to following criteria:
Time period of service                   Bonus
More than 20 years              25%
More than 10 years              15%
>=5 and <=10 years              8%
Less than 5 years                 5%

 

6.   Write a program to find the Factorial of a number

num = int(input(“Enter a number: “))

fact= 1

# check if the number is negative, positive or zero

if num < 0:





   print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
   print("The factorial of 0 is 1")
else:
   for i in range(1,num + 1,1):
       fact= fact*i
   print("Factorial: ",fact)

Output:

Enter a number: 5

Factorial: 120

 

7.   Write a program to find maximum of two numbers using function.

 
 
def maximum(a, b):
           
           if a >= b:
                        return a
           else:
                        return b
           
print(maximum(2, 7))
 
 
Output:

 

7

Method 2: Using inbuilt max() Function

 

a=int(input("Enter 1st number: "))
b=int(input("Enter 2nd number: "))
print("Larger number is", max(a,b))
 
Output:
 
Enter 1st number: 4
Enter 2nd number: 3

Larger number is:  7

 

 

8.   Python Program to Find Area of a Circle using function.

def findArea(r):

    PI = 3.142

    return PI * (r*r);

print(“Area is %.2f” % findArea(5));

 

 

9.   Find the Factorial of a Number Using Function

def factorial(n):

    if n < 0:

        return 0

    elif n == 0 or n == 1:

        return 1

    else:

        fact = 1

        while(n > 1):

            fact *= n

            n -= 1

        return fact

num = 5

print(“Factorial of”,num,”is”,

factorial(num))

Output:

Factorial of 5 is 120

10.           Find the Factorial of a Number Using Recursive approach

def factorial(n):

    if (n==1 or n==0)

          return 1

    else n * factorial(n – 1)

print(“Factorial of”,num,”is”,factorial(5))

Output:

Factorial of 5 is 120

Leave a Comment