Programs · Program 6-11
Q.Program 6-11: Program to find the factors of a whole number using while loop.
Sikkim CbseNCERTSubjective· 3mImportance★★★★★est
38% · 15/39 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →A while loop drives a trial divisor factor from 1 up to the number itself; every value with num % factor == 0 is a factor — for input 12 the program prints 1 2 3 4 6 12.
The idea. A factor of a whole number is any value that divides it leaving remainder 0. The direct algorithm is trial division: test every candidate from 1 to the number. A while loop with an explicitly managed counter (factor) implements this, with the modulus operator doing the divisibility test.
# Program 6-11: find the factors of a whole number using a while loop
num = int(input("Enter a whole number: "))
factor = 1
print("Factors of", num, "are:")
while factor <= num:
if num % factor == 0:
print(factor, end=" ")
factor += 1
Sample run (user enters 12)
Enter a whole number: 12
Factors of 12 are:
1 2 3 4 6 12
Dry run for num = 12 (only the interesting columns)
factor | 12 % factor | Factor? |
|---|---|---|
| 1 | 0 | yes → print 1 |
| 2 | 0 | yes → print 2 |
| 3 | 0 | yes → print 3 |
| 4 | 0 | yes → print 4 |
| 5 | 2 | no |
| 6 | 0 | yes → print 6 |
| 7…11 | non-zero | no |
| 12 | 0 | yes → print 12 |
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.