- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Queries to Print All the Divisors of n using C++

In the given problem, we are required to print all the divisors of a given integer n.

Input: 15 Output: 1 3 5 15 Explanation Divisors of 15 are: 1,3, 5, 15 Input: 30 Output: 1 2 3 5 15 30

In the given problem, we can apply the approach used in the sieve of Eratosthenes for finding all the divisors of n.

## Approach to find The Solution

In the given approach, we will apply the concept in which the sieve of Eratosthenes is based and find the divisors of n.

## Example

#include <bits/stdc++.h> #define MOD 1000000007 using namespace std; vector<int> divisors[100001]; // our vector containing number with all of its divisors void findsieve(int max) { // filling data in vector divisors till 10e5 for(int i = 1; i <= max; i++) { for(int j = i; j <= max; j += i) divisors[j].push_back(i); } } void __print(int n){ // the function to print divisors for(auto x : divisors[n]) cout << x << " "; cout << "\n"; } int main() { findsieve(100000); // we hardcode the sieve and divisors till 10e5 int n = 6; // the given n __print(n); n = 30; // new n __print(n); return 0; }

## Output

1 2 3 6 1 2 3 5 6 10 15 30

## Explanation of the above code

In this approach, we follow the same concept as the sieve of Eratosthenes. We find the divisors of every number till 105. When we are given q queries, we don’t need to find the divisors, so this drastically reduces our time complexity when asked q queries. Hence, our complexity becomes O(Q*N), where Q is the number of queries we tackle, and N is the number of divisors of n.

## Conclusion

In this article, we solve a problem: Queries to print all the divisors of n where we apply the principle of the sieve of Eratosthenes. We also learned the C++ program for this problem and the complete approach ( Normal) by which we solved this problem. We can write the same program in other languages such as C, java, python, and other languages. We hope you find this article helpful.

- Related Questions & Answers
- Divisors of n-square that are not divisors of n in C++ Program
- Find the number of divisors of all numbers in the range [1, n] in C++
- Find largest sum of digits in all divisors of n in C++
- Find sum of divisors of all the divisors of a natural number in C++
- Golang Program to Generate all the Divisors of an Integer
- Print all leaf nodes of an n-ary tree using DFS in C++
- Python program to print a checkboard pattern of n*n using numpy.
- Python program to print check board pattern of n*n using numpy
- Print all Proth primes up to N in C++
- Print all safe primes below N in C++
- Print all multiplicative primes <= N in C++
- How to print a matrix of size n*n in spiral order using C#?
- How to identify and print all the perfect numbers in some closed interval [ 2, n ] using Python?
- Find all divisors of a natural number in java
- Count all perfect divisors of a number in C++