Java program to check prime number in a given range

Java program to check prime number in a given range


import java.util.*;
class PrimeNo
{public static void main()
    {Scanner Sc=new Scanner(System.in);
        System.out.println("Enter the value of s:");
        int s=Sc.nextInt();
        System.out.println("Enter the value of e:");
        int e=Sc.nextInt();
       show(s,e);}
       static  void show(int s,int e)
        {for(int i=s;i<=e;i+=1)
            {System.out.println(i+" is "+isprime(i));
            }
        }
       static  boolean isprime(int n)
        {int c=0;
            for(int i=1;i<=n;i++)
            {if(n%i==0)
                {c++;}}
                if(c==2)
                {return true;}
                else
                {return false;}
            }
        }

Java program to check prime number in a given range


Output;-
Enter the value of s:
10
Enter the value of e:
20
10 is false
11 is true
12 is false
13 is true
14 is false
15 is false
16 is false
17 is true
18 is false
19 is true
20 is false
Java program to check prime number in a given range


N.B;- Here upper range is denoted by 'e' and lower range is denoted by 's'.  The number which is prime is represented by 'true'  and the number which is not prime is represented by 'false'..



Java program to check prime number in a given range.................

No comments:

Powered by Blogger.