Count the primes in a range JAVA - EASY - PART005
Count the primes in a range:
Two whole numbers N1 and N2 are passed as input. The program must print the number of primes present between N1 and N2 (the range is inclusive of N1 and N2)
PROGRAM IN JAVA:
***********************************************************************************
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=0;
for(int i=a;i<=b;i++)
{
int f=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
{f=1;break;}
}
if(f==0 && !(i==1))
c++;
}
System.out.print(c);
}
}
***********************************************************************************
Comments
Post a Comment