0% found this document useful (0 votes)
43 views10 pages

Chapter 3 Simple Python Programs

The document contains various Python programs addressing different problems, including calculating sums of positive and negative numbers, finding the largest even and odd numbers, forming a new integer based on digit count, and checking for Armstrong, Perfect, and Strong numbers. Each program includes a problem description, solution steps, source code, and runtime test cases. The document serves as a comprehensive guide for learning and practicing Python programming concepts.
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)
43 views10 pages

Chapter 3 Simple Python Programs

The document contains various Python programs addressing different problems, including calculating sums of positive and negative numbers, finding the largest even and odd numbers, forming a new integer based on digit count, and checking for Armstrong, Perfect, and Strong numbers. Each program includes a problem description, solution steps, source code, and runtime test cases. The document serves as a comprehensive guide for learning and practicing Python programming concepts.
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
You are on page 1/ 10

Leela Soft 1000 MCQ Programs Madhu

Problem Solution
1. Take in the number of elements to be in the list from the user.
2. Take in the elements from the user using a for loop and append to a list.
3. Using a for loop, get the elements one by one from the list and check if it is positive or
negative.
4. If it is positive, check if it is odd or even and find the individual sum.
5. Find the individual sum of negative numbers.
6. Print all the sums.
7. Exit.

Program/Source Code
Here is source code of the Python Program to print the sum of negative numbers, positive even
numbers and positive odd numbers in a given list. The program output is also shown below.

n = int(input("Enter the number of elements to be in the list:"))


b = []
for i in range(0, n):
a = int(input("Element: "))
b.append(a)
sum1 = 0
sum2 = 0
sum3 = 0
for j in b:
if(j > 0):
if(j % 2 == 0):
sum1 = sum1 + j
else:
sum2 = sum2 + j
else:
sum3 = sum3 + j
print("Sum of all positive even numbers:", sum1)
print("Sum of all positive odd numbers:", sum2)
print("Sum of all negative numbers:", sum3)

Program Explanation
1. User must enter the number of elements to be in the list.
2. User must then enter the elements of the list one by one which is appended to the list.
3. The for loop is used to traverse through the list and obtain the elements one by one.
4. The if statement checks whether the element is positive or negative.
5. If it is positive, another if statement checks if it is odd or even.
6. The sum of even elements is stored in sum1 and the sum of odd elements is stored in
sum2.
7. The sum of negative numbers is stored in sum3.
8. All the sums are respectively printed.

www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Runtime Test Cases


Case 1:
Enter the number of elements to be in the list:4
Element: -12
Element: 34
Element: 35
Element: 89
Sum of all positive even numbers: 34
Sum of all positive odd numbers: 124
Sum of all negative numbers: -12

Case 2:
Enter the number of elements to be in the list:5
Element: -45
Element: -23
Element: 56
Element: 23
Element: 7
Sum of all positive even numbers: 56
Sum of all positive odd numbers: 30
Sum of all negative numbers: -68

Python Program to Print Largest Even and Largest Odd Number in a List
Problem Description
The program takes in a list and prints the largest even and largest off number in it.

Problem Solution
1. Take in the number of elements to be in the list from the user.
2. Take in the elements from the user using a for loop and append to a list.
3. Using a for loop, get the elements one by one from the list and check if it odd or even and
append them to different lists.
4. Sort both the lists individually and get the length of each list.
5. Print the last elements of the sorted lists.
6. Exit.

Program/Source Code
Here is source code of the Python Program to print the largest even and largest odd number in a
list. The program output is also shown below.

www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
n = int(input("Enter the number of elements to be in the list:"))
b = []
for i in range(0, n):
a = int(input("Element: "))
b.append(a)
c = []
d = []
for i in b:
if(i % 2 == 0):
c.append(i)
else:
d.append(i)
c.sort()
d.sort()
count1 = 0
count2 = 0
for k in c:
count1 = count1 + 1
for j in d:
count2 = count2 + 1
print("Largest even number:", c[count1 - 1])
print("Largest odd number", d[count2 - 1])

Program Explanation
1. User must enter the number of elements to be in the list.
2. User must then enter the elements of the list one by one which is appended to the list.
3. The for loop is used to traverse through the list and obtain the elements one by one.
4. The if statement checks whether the element is odd or even.
5. The odd elements are appended to another list and the even elements are appending to
another list.
6. Both the lists are then sorted.
7. The individual lengths of both the lists is obtained using for loops.
8. The last element of the sorted lists which is the largest odd and largest even number is
printed.

Runtime Test Cases


Case 1:
Enter the number of elements to be in the list:5
Element: 45
Element: 20
Element: 80
Element: 93
Element: 3
Largest even number: 80
Largest odd number 93

www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Case 2:
Enter the number of elements to be in the list:4
Element: 23
Element: 10
Element: 34
Element: 89
Largest even number: 34
Largest odd number 89

Python Program to Form an Integer that has the Number of Digits at Ten's Place and the Least
Significant Digit of the Entered Integer at One's Place
Problem Description
The program takes an integer and forms a new integer which has the number of digits at the ten’s
place and the least significant digit in the one’s place.

Problem Solution
1. Take in an integer and store it in a variable.
2. Make a separate copy of the integer.
3. Using a while loop, calculate the number of digits in the integer.
4. Convert the copy of the integer and the count of the number of digits to string.
5. Concatenate the last digit of the integer to the count and convert the new integer back
to int.
6. Print the newly formed integer.
7. Exit.

Program/Source Code
Here is source code of the Python Program tto form an integer that has the number of digits at
the ten’s place and the most significant digit in the one’s place. The program output is also shown
below.

n = int(input("Enter the number:"))


tmp = n
k = 0
while(n > 0):
k = k + 1
n = n // 10
b = str(tmp)
c = str(k)
d = c + b[k - 1]
print("The new number formed:", int(d))

www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Program Explanation
1. User must enter the number and store it in a variable.
2. A copy of the variable is made because the original value of the variable would be changed
for counting the number of digits.
3. The while loop is used and the last digit of the number is obtained by using the modulus
operator.
4. Each time a digit is obtained, the count value is incremented.
5. The number of digits of the variable and the number is converted to string for ease of
concatenation.
6. Then the last digit of the string is obtained and concatenated to the count variable.
7. The new number formed is then printed.

Runtime Test Cases


Case 1:
Enter the number:129
The new number formed: 39

Case 2:
Enter the number:24678
The new number formed: 58

Python Program to Find Those Numbers which are Divisible by 7 and Multiple of 5 in a Given
Range of Numbers
Problem Description
The program takes an upper range and lower range and finds those numbers within the range
which are divisible by 7 and multiple of 5.

Problem Solution
1. Take in the upper and lower range and store it in separate variables.
2. Use a for loop which ranges from the lower range to the upper range.
3. Then find the numbers which are divisible by both 5 and 7.
4. Print those numbers
5. Exit.

Program/Source Code
Here is source code of the Python Program to find those numbers which are divisible by 7 and
multiple of 5 in a given range of numbers. The program output is also shown below.

www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
lower = int(input("Enter the lower range:"))
upper = int(input("Enter the upper range:"))
for i in range (lower, upper + 1):
if(i % 7 == 0 and i % 5 == 0):
print(i)

Program Explanation
1. User must enter the upper range limit and the lower range limit.
2. Then the for loop ranges from the lower limit to the upper limit.
3. The value of i ranges from the lower limit to the upper limit.
4. Whenever the remainder of i divided by 5 and 7 is equal to 0, i is printed.

Runtime Test Cases


Case 1:
Enter the lower range:1
Enter the upper range:100
35
70

Case 2:
Enter the lower range:400
Enter the upper range:700
420
455
490
525
560
595
630
665
700

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.

www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

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.

Runtime Test Cases


Case 1:
Enter any number: 13
The number isn't an arsmtrong number.

Case 2:
Enter any number: 371
The number is an armstrong number.

Python Program to Print the Pascal's triangle for n number of rows given by the user
Problem Description
The program takes a number n and prints the pascal’s triangle having n number of rows.

www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Problem Solution
1. Take in the number of rows the triangle should have and store it in a separate variable.
2. Using a for loop which ranges from 0 to n-1, append the sub-lists into the list.
3. Then append 1 into the sub-lists.
4. Then use a for loop to determine the value of the number inside the triangle.
5. Print the Pascal’s triangle according to the format.
6. Exit.

Program/Source Code
Here is source code of the Python Program to print the pascal’s triangle for n number of rows
given by the user. The program output is also shown below.

n = int(input("Enter number of rows: "))


a = []
for i in range(n):
a.append([])
a[i].append(1)
for j in range(1, i):
a[i].append(a[i - 1][j - 1] + a[i - 1][j])
if(n != 0):
a[i].append(1)
for i in range(n):
print(" "*(n - i), end=" ", sep=" ")
for j in range(0, i + 1):
print('{0:6}'.format(a[i][j]), end=" ", sep=" ")
print()

Program Explanation
1. User must enter the number of rows that the Pascal’s triangle should have.
2. The for loop is used to append sub-lists into an empty list defined earlier.
3. Then 1 is appended into all the sub-lists.
4. The for loop is used to determine the value of the number inside the triangle which is the
sum of the two numbers above it.
5. The other for loop is used to print the Pascal’s triangle according to the format.

Runtime Test Cases


Case 1:
Enter number of rows: 3
1
1 1
1 2 1

www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Case 2:
Enter number of rows: 4
1
1 1
1 2 1
1 3 3 1

Python Program to Check if a Number is a Perfect Number


Problem Description
The program takes a number and checks if it is a Perfect number.

Problem Solution
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.

Program/Source Code
Here is source code of the Python Program to check if a number is a Perfect number. The program
output is also shown below.

n = int(input("Enter any number: "))


sum1 = 0
for i in range(1, n):
if(n % i == 0):
sum1 = sum1 + i
if (sum1 == n):
print("The number is a Perfect number!")
else:
print("The number is not a Perfect number!")

Program Explanation
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).

www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

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.

Runtime Test Cases


Case 1:
Enter any number: 6
The number is a Perfect number!

Case 2:
Enter any number: 25
The number is not a Perfect number!

Python Program to Check if a Number is a Strong Number


Problem Description
The program takes a number and checks if it is a strong number.

Problem Solution
1. Take in an integer and store it in a variable.
2. Using two while loops, find the factorial of each of the digits in the number.
3. Then sum up all the factorials of the digits.
4. Check if the sum of the factorials of the digits is equal to the 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 a strong number. The program
output is also shown below.

sum1 = 0
num = int(input("Enter a number:"))
temp = num
while(num):
i = 1
f = 1
r = num % 10
while(i <= r):
f = f * i

www.leelasoft.com Cell: 78 42 66 47 66

You might also like