Problem Statement
We need to write a program that accepts a positive integer value from the user and prints all the possible divisors of that number (excluding the number itself).
For example
Input: 100 Output : 1 2 4 5 10 25 50
Before we move to the algorithm and programs to find the divisors of a positive number, let us first have a brief introduction to what a divisor is.
What is a Divisor?
A number that divides an integer exactly without leaving any remainder is called a divisor.
For example, 2 is the divisor of 10, as 10 ÷ 2 = 5, exactly without leaving no remainder. However, 3 is not the divisor of 10, as 10 ÷ 3 = 3, leaving 1 as a remainder.
Note: A divisor is also a factor of an original integer.
Algorithm
The problem statement is quite straightforward and beginner-level. We only need a loop and a conditional statement to solve this problem. As the problem statement states, all we need to do is find all the divisors except for the number itself.
So we need a loop from range 1 to half of the number entered by the user because a number's all possible divisors can be the number itself and less than half of it.
C Program to Find the Divisors of a Positive Integer
Output
C++ Program to Find the Divisors of a Positive Integer
Output:
Python Program to Find the Divisors of a Positive Integer
Output:
Java Program to Find the Divisors of a Positive Integer
Output:
Wrapping Up!
Now let's wrap up this programming tutorial on "Write a program to find the divisors of a positive integer". To find all the divisors of a number
n
, we need to calculate all the smaller numbers that can divide the number
n
completely.
If we look closely, all the divisors of a number lie between 1 to half of the number itself, saving time, and we need to create a loop that would iterate n/2 time.
People are also reading:
- Data Encapsulation in C++
- WAP in C++ & Python to Calculate Compound Interest
- Interfaces in C++
- WAP in C++ & Python to calculate the size of each data types
- C++ Strings
- WAP to calculate the average marks of 5 subjects
- Variable Scope in C++
- WAP in C++ & Python for Armstrong Number
- C++ Arrays
- WAP in C++ & Python to reverse a number
- Modifier Types in C++
Leave a Comment on this Post