C++ program to find number of integers which has exactly x divisors
November 2, 2021
Number of integers which has exactly X divisors
In this page we will learn how to find number of integers which has exactly x divisors. Divisors are numbers which perfectly divides a number. For example take 6 as a number then divisors of 6 are 1,2,3,6. ‘1’ and number itself are always divisors of the same number. If a number has exactly 2 divisors then that number is a prime number.
Here our task is to write a C program to count out how many number of integers which has exactly x divisors. This can be done by finding number of divisors that each number has and then checking with the given ‘X‘. If X and number of divisors match then we will increment the counter value.
Example:
Input
Number=30
X=3
Output
numbers with exactly 3 divisors are 4 9 25
count= 3
Algorithm
Take user inputs like Number and X.
Initialize a count variable with zero value.
Run a for loop with a range from 1 to Number+1.
Initialize another count variable with zero.
Run other for loop ranging from 1 to iterator of 1st for loop+1.
Check for complete division conditions and if TRUE increment count2 by 1.
Come out of for loop and check if count2 is equal to Divisor.
If TRUE increment count1 by 1 and print the number with exact divisors.
Print count1.
C++ Code:-
#include <iostream>
using namespace std;
int main() { int Number,X,count1; cout<<"Enter range of number :"; cin>>Number; cout<<"\nEnter exact number of divisors :"; cin>>X; count1 = 0; for(int i=1;i<=Number;i++) { int count2 = 0; for(int j=1;j<=i;j++) { if(i%j==0) { count2++; } } if(count2==X) { count1++; cout<<i; } } cout<<count1; return 0; }