- 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

# Find the number of divisors of all numbers in the range [1, n] in C++

In this problem, we are given a number N. Our task is to *find the number of divisors of all numbers in the range [1, n].*

**Let’s take an example to understand the problem,**

Input : N = 7 Output : 1 2 2 3 2 4 2

## Solution Approach

A simple solution to the problem is by starting from 1 to N and for every number count the number of divisors and print them.

## Example 1

Program to illustrate the working of our solution

#include <iostream> using namespace std; int countDivisor(int N){ int count = 1; for(int i = 2; i <= N; i++){ if(N%i == 0) count++; } return count; } int main(){ int N = 8; cout<<"The number of divisors of all numbers in the range are \t"; cout<<"1 "; for(int i = 2; i <= N; i++){ cout<<countDivisor(i)<<" "; } return 0; }

## Output

The number of divisors of all numbers in the range are 1 2 2 3 2 4 2 4

Another approach to solve the problem is using increments of values. For this, we will create an array of size (N+1). Then, starting from 1 to N, we will check for each value i, we will increment the array value for all multiples of i less than n.

## Example 2

Program to illustrate the working of our solution,

#include <iostream> using namespace std; void countDivisors(int N){ int arr[N+1]; for(int i = 0; i <= N; i++) arr[i] = 1; for (int i = 2; i <= N; i++) { for (int j = 1; j * i <= N; j++) arr[i * j]++; } for (int i = 1; i <= N; i++) cout<<arr[i]<<" "; } int main(){ int N = 8; cout<<"The number of divisors of all numbers in the range are \t"; countDivisors(N); return 0; }

## Output

The number of divisors of all numbers in the range are 1 2 2 3 2 4 2 4

- Related Questions & Answers
- Find sum of divisors of all the divisors of a natural number in C++
- Program to find count of numbers having odd number of divisors in given range in C++
- Find all divisors of a natural number in java
- Python - Find the number of prime numbers within a given range of numbers
- Find all divisors of a natural number - Set 1 in C++
- Find all divisors of a natural number - Set 2 in C++
- Find the largest good number in the divisors of given number N in C++
- Program to find out the sum of the number of divisor of the divisors in Python
- Find numbers with K odd divisors in a given range in C++
- Count all perfect divisors of a number in C++
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors in C++
- Sum of all proper divisors of a natural number in java
- Program to find out the number of special numbers in a given range in Python
- Count the number of common divisors of the given strings in C++
- Program to count number of common divisors of two numbers in Python

Advertisements