C program to find number of integers which has exactly x divisors Number of integers which has exactly X divisors using C

In this page we will learn how to find number of integers which has exactly x divisors using  C . 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. Code to find the number of integers which has exactly x divisors in C :

#include<stdio.h>

int main()

{

int Number,X,count1;

printf("\nEnter range of number :");

scanf("%d",&Number);

printf("\nEnter exact number of divisors :");

scanf("%d",&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++;

printf("%d ",i);

}

}

printf("\n%d",count1);

return 0;

}

Input:

Enter range of number : 30
Enter exact number of divisors : 3

4 9 25
3