Python Programs
1.Python Program to Calculate Grade of a Student
This is a Python Program to take in the marks of 5 subjects and display the grade.
Problem Description:- The program takes in the marks of 5 subjects and displays the grade.
Problem Solution (Algorithm)
1. Take in the marks of 5 subjects from the user and
store it in different variables.
2. Find the average of the marks.
3. Use an else condition to decide the grade based on the average of the marks.
4. Exit.
Program/Source Code
Here is source code of the Python Program to take in the marks of 5 subjects and display the grade. The program output is also shown below.
sub1=int(input("Enter marks of the first subject: "))
sub2=int(input("Enter marks of the second subject: "))
sub3=int(input("Enter marks of the third subject: "))
sub4=int(input("Enter marks of the fourth subject: "))
sub5=int(input("Enter marks of the fifth subject: "))
avg=(sub1+sub2+sub3+sub4+sub4)/5
if(avg>=90):
print("Grade: A")
elif(avg>=80&avg<90):
print("Grade: B")
elif(avg>=70&avg<80):
print("Grade: C")
elif(avg>=60&avg<70):
print("Grade: D")
else:
print("Grade: F")
Program Explanation
1. User must enter 5 different values and store it in
separate variables.
2. Then sum up all the five marks and divide by 5 to find the average of the
marks.
3. If the average is greater than 90, “Grade: A” is printed.
4. If the average is in between 80 and 90, “Grade: B” is printed.
5. If the average is in between 70 and 80, “Grade: C” is printed.
6. If the average is in between 60 and 70, “Grade: D” is printed.
7. If the average is anything below 60, “Grade: F” is printed.
2.Python Program to Check Prime Number
This is a Python Program to check if a number is a prime number.
Problem Description
The program takes in a number and checks if it is a prime number.
Problem Solution
1. Take in the
number to be checked and store it in a variable.
2. Initialize the count variable to 0.
3. Let the for loop range from 2 to half of the number (excluding 1 and the
number itself).
4. Then find the number of divisors using the if statement and increment the
count variable each time.
5. If the number of divisors is lesser than or equal to 0, the number is prime.
6. Print the final result.
5. Exit.
Program/Source Code
Here is source code of the Python Program to check if a number is a prime number. The program output is also shown below.
a=int(input("Enter number: "))
k=0
for i in range(2,a//2+1):
if(a%i==0):
k=k+1
if(k<=0):
print("Number is prime")
else:
print("Number isn't prime")
Program Explanation
1. User must enter the number to be
checked and store it in a different variable.
2. The count variable is first initialized to 0.
3. The for loop ranges from 2 to the half of the number so 1 and the number
itself aren’t counted as divisors.
4. The if statement then checks for the divisors of the number if the remainder
is equal to 0.
5. The count variable counts the number of divisors and if the count is lesser
or equal to 0, the number is a prime number.
6. If the count is greater than 0, the number isn’t prime.
7. The final result is printed.
3.Python Program to Check Armstrong Number
This is a Python Program to check if a number is an Armstrong number.
Problem Description
The program takes a number and checks if it is an Armstrong number.
Problem Solution
1. Take in an
integer and store it in a variable.
2. Convert each digit of the number to a string variable and store it in a
list.
3. Then cube each of the digits and store it in another list.
4. Find the sum of the cube of digits in the list and check if it is equal to
the original number.
5. Print the final result.
6. Exit.
Program/Source Code
Here is source code of the Python Program to check if a number is an Armstrong number. The program output is also shown below.
n=int(input("Enter any number: "))
a=list(map(int,str(n)))
b=list(map(lambda x:x**3,a))
if(sum(b)==n):
print("The number is an armstrong number. ")
else:
print("The number isn't an arsmtrong number. ")
Program Explanation
1. User must enter the number and store it
in a variable.
2. The map function obtains each digit from the number and converts it to a
string and stores it in a list.
3. The second map function cubes each digit and stores it in another list.
4. Then the sum of the cubes of the digits is found and is checked if it is
equal to the original number.
5. If the sum of the cube of digits is equal to the original number, the number
is an Armstrong number.
6. The final result is printed.
4.Python Program to Check Whether a Given Number is Perfect Number
2. This is a Python Program to check if a number is a Perfect number.
3. Problem Description
4. The program takes a number and checks if it is a Perfect number.
5. Problem Solution
6. 1. Take in an integer and store it in a
variable.
2. Initialize a variable to count the sum of the proper divisors to 0.
3. Use a for loop and an if statement to add the proper divisors of the integer
to the sum variable.
4. Check if the sum of the proper divisors of the number is equal to the
variable.
5. Print the final result.
6. Exit.
7. Program/Source Code
8. Here is source code of the Python Program to check if a number is a Perfect number. The program output is also shown below.
9.
10.n = int(input("Enter any number: "))
11.sum1 = 0
12.for i in range(1, n):
13. if(n % i == 0):
14. sum1 = sum1 + i
15.if (sum1 == n):
16. print("The number is a Perfect number!")
17.else:
18. print("The number is not a Perfect number!")
19. Program Explanation
20. 1. User must enter the number and store
it in a variable.
2. Use a for loop to generate numbers from 1 to n (where n is not included as
we need the sum of the proper divisors of the number).
3. Using an if statement check if the number divided by i gives the remainder
as 0 which is basically the proper divisor of the integer.
4. Then the proper divisors of the number are added to the sum variable.
5. If the sum of the proper divisors of the number is equal to the original
number, tje number is a Perfect number.
6. The final result is printed.
5.Python Program to Print Table of a Given Number
This is a Python Program to print the table of a given number.
Problem Description
The program takes in a number and prints the table of a given number.
Problem Solution
1. Take in a
number and store it in a variable.
2. Print the multiplication tables of a given number.
3. Exit.
Program/Source Code
Here is source code of the Python Program to print the table of a given number. The program output is also shown below.
n=int(input("Enter the number to print the tables for:"))
for i in range(1,11):
print(n,"x",i,"=",n*i)
Program Explanation
1. User must
enter a number.
2. Using a print statement, print the multiplication tables of the given
number.
Runtime Test Cases
Case 1:
Enter the number to print the tables for:7
7 x 1 = 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
Case 2:
Enter the number to print the tables for:17
17 x 1 = 17
17 x 2 = 34
17 x 3 = 51
17 x 4 = 68
17 x 5 = 85
17 x 6 = 102
17 x 7 = 119
17 x 8 = 136
17 x 9 = 153
17 x 10 = 170
6.Sum of Digits Program in Python
This is a Python Program to find the sum of digits in a number.
Problem Description
The program takes in a number and finds the sum of digits in a number.
Problem Solution
1. Take the
value of the integer and store in a variable.
2. Using a while loop, get each digit of the number and add the digits to a
variable.
3. Print the sum of the digits of the number.
4. Exit.
Program/Source Code
Here is the source code of the Python Program to find the sum of digits in a number. The program output is also shown below.
n=int(input("Enter a number:"))
tot=0
while(n>0):
dig=n%10
tot=tot+dig
n=n//10
print("The total sum of digits is:",tot)
Program Explanation
1. User must
first enter the value and store it in a variable.
2. The while loop is used and the last digit of the number is obtained by using
the modulus operator.
3. The digit is added to another variable each time the loop is executed.
4. This loop terminates when the value of the number is 0.
5. The total sum of the number is then printed.
Runtime Test Cases
Case 1:
Enter a number:1892
The total sum of digits is: 20
Case 2:
Enter a number:157
The total sum of digits is: 13
7.Python Program to Print All Odd Numbers in a Range
This is a Python Program to print odd numbers within a given range.
Problem Description
The program takes the upper and lower limit and prints all odd numbers within a given range.
Problem Solution
1. Take in the
upper range limit and the lower range limit and store it in separate variables.
2. Use a for-loop ranging from the lower range to the upper range limit.
3. Then use an if statement if check whether the number is odd or not and print
the number.
4. Exit.
Program/Source Code
Here is the source code of the Python Programto print odd numbers within a given range. The program output is also shown below.
lower=int(input("Enter the lower limit for the range:"))
upper=int(input("Enter the upper limit for the range:"))
for i in range(lower,upper+1):
if(i%2!=0):
print(i)
Program Explanation
1. User must
enter the upper range limit and the lower range limit.
2. The for loop ranges from the lower range limit to the upper range limit.
3. The expression within the if-statement checks if the remainder obtained when
the number divided by 2 is one or not (using the % operator).
4. If the remainder isn’t equal to 0, the number is odd and hence the number is
printed.
Runtime Test Cases
Case 1:
Enter the lower limit for the range:1
Enter the upper limit for the range:16
1
3
5
7
9
11
13
15
Case 2:
Enter the lower limit for the range:150
Enter the upper limit for the range:180
151
153
155
157
159
161
163
165
167
169
171
173
175
177
179
8.Python Program to Check Whether a given Year is a Leap Year
This is a Python Program to check whether a given year is a leap year or not.
Problem Description
The program takes in a year and checks whether it is a leap year or not.
Problem Solution
1. Take the
value of the year as input
2. Using an if-statement, check whether the year is a leap year or not
3. Print the final result
4. Exit
Program/Source Code
Here is source code of the Python Program to Calculate the Average of Numbers in a Given List. The program output is also shown below.
year=int(input("Enter year to be checked:"))
if(year%4==0 and year%100!=0 or year%400==0):
print("The year is a leap year!")
else:
print("The year isn't a leap year!")
Program Explanation
1. User must
first enter the year to be checked.
2. The if statement checks if the year is a multiple of 4 but isn’t a multiple
of 100 or if it is a multiple of 400 (not every year that is a multiple of 4 is
a leap year).
3. Then the result is printed.
Runtime Test Cases
Case 1:
Enter year to be checked:2016
The year is a leap year!
Case 2:
Enter year to be checked:2005
The year isn't a leap year!
9. Python Program to Find the LCM of Two Numbers
This is a Python Program to find the LCM of two numbers.
Problem Description
The program takes two numbers and prints the LCM of two numbers.
Problem Solution
1. Take in both
the integers and store it in separate variables.
2. Find which of the integer among the two is smaller and store it in a
separate variable.
3. Use a while loop whose condition is always True until break is used.
4. Use an if statement to check if both the numbers are divisible by the
minimum number and increment otherwise.
5. Print the final LCM.
6. Exit.
Program/Source Code
Here is source code of the Python Program to find the LCM of two numbers. The program output is also shown below.
a=int(input("Enter the first number:"))
b=int(input("Enter the second number:"))
if(a>b):
min1=a
else:
min1=b
while(1):
if(min1%a==0 and min1%b==0):
print("LCM is:",min1)
break
min1=min1+1
Program Explanation
1. User must
enter both the numbers and store it in separate variables.
2. An if statement is used to find out which of the numbers is smaller and
store in a minimum variable.
3. Then a while loop is used whose condition is always true (or 1) unless break
is used.
4. Then an if statement within the loop is used to check whether the value in
the minimum variable is divisible by both the numbers.
5. If it is divisible, break statement breaks out of the loop.
6. If it is not divisible, the value in the minimum variable is incremented.
7. The final LCM is printed.
10. Python Program to Find the GCD of Two Numbers
This is a Python Program to find the GCD of two numbers.
Problem Description
The program takes two numbers and prints the GCD of two numbers.
Problem Solution
1. Import the fractions
module.
2. Take in both the integers and store it in separate variables.
3. Use the in-built function to find the GCD of both the numbers.
4. Print the GCD.
5. Exit.
Program/Source Code
Here is source code of the Python Program to find the GCD of two numbers. The program output is also shown below.
import fractions
a=int(input("Enter the first number:"))
b=int(input("Enter the second number:"))
print("The GCD of the two numbers is",fractions.gcd(a,b))
Program Explanation
1. Import the
fractions module.
2. User must enter both the numbers and store it in separate variables.
3. The in-built function of fractions.gcd(a,b) is used where a and b are the
variables containing the integer values.
4. The final GCD is printed.
Runtime Test Cases
Case 1:
Enter the first number:15
Enter the second number:5
The GCD of the two numbers is 5
Case 2:
Enter the first number:105
Enter the second number:222
The GCD of the two numbers is 3
Python Notes Web Desigining Notes