INTRODUCTION
Given an integer n, write a program TO PRINT ALL DIVISORS OF A GIVEN NUMBER.
Here’s an algorithm to find all divisors of a given number:
- Start a loop from 1 to the given number.
- For each iteration, check if the current number (i) is a divisor of the given number by using the modulus operator (n%i).
- If the modulus is 0, it means i is a divisor of n, so print i.
- Continue looping until the end of the range.
It’s worth noting that the above algorithm will return both the divisors and the number itself, for example, for the number 12, it will return 1,2,3,4,6,12
Ex1:
Input N: 12
Output: 1 2 3 4 6 12.
APPROACH1
To print all the divisors of a given number, we will check whether each number starting from 1 to the given number divides the input number or not. If any number divides the input number without leaving any remainder, then it is considered a divisor of the given number. To perform the above operation, we use any kind of loop.
ALGORITHM
STEP 1: start
STEP 2: take the input value of n.
STEP 3: check whether each number starting from 1 to the input number divides the input number without leaving any remainder.
STEP 4: if the remainder resulted in zero, it is considered a divisor and print that number.
STEP 5: stop.
C++ Code:
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
for(int i = 1 ; i<=n ; i++){
if(n%i == 0)
cout<<i<<" ";
}
}
Python code:
n = int(input())
for i in range(1,n+1):
if n%i == 0:
print(i,end=' ')
JAVA code:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
for(int i = 1 ; i<=n ; i++){
if(n%i == 0)
System.out.print(i+" ");
}
}
}
Input: 12
Output: 1 2 3 4 6 12
The time complexity of the code is O(n) as here we use a for loop.
The space complexity of the code is O(1).
APPROACH 2:
Applying the above same logic but iterating the loop from 1 to less than or equal to the square root of the given number, results in decreased time complexity of the code.
ALGORITHM
STEP 1: start
STEP 2: take the input value of n.
STEP 3: check whether each number starting from 1 to the square root of the input number divides the input number without leaving any remainder.
STEP 4: if the remainder resulted in zero, it is considered as a divisor and print that number. And also, check whether the resultant of the division of the input number with that particular number at that iteration is not equal to the particular number at that iteration, if it is not equal, then print the resultant of the division of the input number with that particular number at that instant.
STEP 5: stop.
C++ Code:
#include <iostream>
#include<cmath>
using namespace std;
int main() {
int n;
cin>>n;
for(int i = 1 ; i<=sqrt(n) ; i++){
if(n%i == 0) {
cout<<i<<" ";
if(i != n/i)
cout<<n/i<<" ";
}
}
}
Python code:
import math
n = int(input())
end = math.floor(math.sqrt(n))
for i in range(1,end+1):
if n%i == 0:
print(i,end=' ')
if i!=n/i:
print(int(n/i),end=' ')
JAVA code:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
for(int i = 1 ; i<=Math.sqrt(n) ; i++){
if(n%i == 0){
System.out.print(i+" ");
if(i != n/i)
System.out.print(n/i+" ");
}
}
}
}
Input: 12
Output: 1 12 2 6 3 4
The time complexity is O(√n) and the space complexity of the code is O(1).
CONCLUSION
That’s it from this tutorial. Hope you guys found It interesting. We have solved the problem to print all the divisors for the given number in different programming languages. If you have any queries please leave a comment below!
Happy coding!