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

Simple Python Programs Set 2

The document contains multiple Python programs that demonstrate various programming concepts, including calculating quotients and remainders, generating combinations of digits, printing odd numbers within a range, summing digits of a number, finding the smallest divisor, counting digits, checking for palindromes, and filtering integers based on divisibility. Each program includes a problem description, solution steps, and runtime test cases. The content is structured to provide practical examples for learning Python programming.

Uploaded by

pythonatmadhu
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)
2 views6 pages

Simple Python Programs Set 2

The document contains multiple Python programs that demonstrate various programming concepts, including calculating quotients and remainders, generating combinations of digits, printing odd numbers within a range, summing digits of a number, finding the smallest divisor, counting digits, checking for palindromes, and filtering integers based on divisibility. Each program includes a problem description, solution steps, and runtime test cases. The content is structured to provide practical examples for learning Python programming.

Uploaded by

pythonatmadhu
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

Leela Soft 1000 MCQ Programs Madhu

Case 2:
Enter the first number: 125
Enter the second number: 7
Quotient is: 17
Remainder is: 6

Python Program to Accept Three Digits and Print all Possible Combinations from the Digits
Problem Description
The program takes three distinct numbers and prints all possible combinations from the digits.

Problem Solution
1. Take in the first, second and third number and store it in separate variables.
2. Then append all the three numbers to the list.
3. Use three for loops and print the digits in the list if none of their indexes are equal to each
other.
4. Exit.

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


b = int(input("Enter second number:"))
c = int(input("Enter third number:"))
d = []
[Link](a)
[Link](b)
[Link](c)
for i in range(0, 3):
for j in range(0, 3):
for k in range(0, 3):
if(i != j & j != k & k != i):
print(d[i], d[j], d[k])

Runtime Test Cases


Case 1:
Enter first number:1
Enter second number:2
Enter third number:3
123
132
213
231

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

312
321

Case 2:
Enter first number:5
Enter second number:7
Enter third number:3
573
537
753
735
357
375

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.

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)

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

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

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

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.

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)

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

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

Python Program to Find the Smallest Divisor of an Integer


Problem Description
The program takes in an integer and prints the smallest divisor of the integer.

Problem Solution
1. Take in an integer from the user.
2. Use a for loop where the value of i ranges from 2 to the integer.
3. If the number is divisible by i, the value of i is appended to the list.
4. The list is then sorted and the smallest element is printed.
5. Exit.

n = int(input("Enter an integer:"))
a = []
for i in range(2, n + 1):
if(n % i == 0):
[Link](i)
[Link]()
print("Smallest divisor is:", a[0])

Runtime Test Cases


Case 1:
Enter an integer:75
Smallest divisor is: 3

Case 2:
Enter an integer:64
Smallest divisor is: 2

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Python Program to Count the Number of Digits in a Number


Problem Description
The program takes the number and prints the number of digits in the 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 increment the count each time a
digit is obtained.
3. Print the number of digits in the given integer.
4. Exit.

n = int(input("Enter number:"))
count = 0
while(n > 0):
count = count + 1
n = n // 10
print("The number of digits in the number are:", count)

Runtime Test Cases


Case 1:
Enter number:123
The number of digits in the number are: 3

Case 2:
Enter number:1892
The number of digits in the number are: 4

Python Program to Check if a Number is a Palindrome


Problem Description
The program takes a number and checks whether it is a palindrome or not.

Problem Solution
1. Take the value of the integer and store in a variable.
2. Transfer the value of the integer into another temporary variable.
3. Using a while loop, get each digit of the number and store the reversed number in another
variable.
4. Check if the reverse of the number is equal to the one in the temporary variable.
5. Print the final result.
6. Exit.

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
n = int(input("Enter number:"))
temp = n
rev = 0
while(n > 0):
dig = n % 10
rev = rev * 10 + dig
n = n // 10
if(temp == rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")

Runtime Test Cases


Case 1
Enter number:121
The number is a palindrome!

Case 2
Enter number:567
The number isn't a palindrome!

Python Program to Print all Integers that Aren't Divisible by Either 2 or 3 and Lie between 1 and
50.
Problem Description
The program prints all integers that aren’t divisible by either 2 or 3 and lies between 1 and 50.

Problem Solution
1. Use a for-loop ranging from 0 to 51.
2. Then use an if statement to check if the number isn’t divisible by both 2 and 3.
3. Print the numbers satisfying the condition.
4. Exit.

for i in range(0, 51):


if(i % 2 != 0 and i % 3 != 0):
print(i)

Runtime Test Cases


Case 1:
1
5
7
11
13

[Link] Cell: 78 42 66 47 66

You might also like