Given a starting number and the ending number the task is to find the all the Composite Number present in that given range and also to display it’s frequency.
Composite Number is a number which have more than two factors.
Example 1: 4 have three factors (1,2,4). Hence it is a Composite Number.
Example 2: 12 have six factors (1,2,3,4,6,12). Hence it is a Composite Number.
Java
import java.util.*;
public class CompositeNumberRange
{
public static void main(String args[])
{
int start=0,end=0,flag=0,frequency=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a starting range: ");
start=sc.nextInt();
System.out.print("Enter a ending range: ");
end=sc.nextInt();
System.out.println("Composite Numbers in the given range are:-");
for(int i=start;i<=end;i++)
{
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
flag++;
}
}
if(flag!=2)
{
System.out.println(i);
frequency++;
}
flag=0;
}
System.out.println("Frequency: "+frequency);
}
}Java