C++ program to find number of integers which has exactly x divisors
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.
- numbers with exactly 3 divisors are 4 9 25
- count= 3
- 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.
using namespace std;
cout<<"Enter range of number :";
cout<<"\nEnter exact number of divisors :";
count1 = 0;
int count2 = 0;
Enter range of number :30
Enter exact number of divisors :3