PRIME NO BETWEEN ANY TWO GIVEN NUMBER

package prime.no.btwn.any.two.no;
import java.util.Scanner;
public class PrimeNoBtwnAnyTwoNo {
void prime_no_cll()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1st no");
int n1 = sc.nextInt();
System.out.println("Enter 2nd no");
int n2 = sc.nextInt();
int rem = 0;
if(n1>n2)                                         //swapping of digit in cace of descending order user input
{
int valuestorebox=0;
valuestorebox=n2;
n2=n1;
n1=valuestorebox;
System.out.println(" now n1 is : "+ n1 +"\n n2 is : "+ n2+" \nand temp value store box is : "+ valuestorebox);
}
for (int i = n1; i <= n2; i++)
{
int count = 0;
for (int j = 2; j < i; j++)
{
rem = (i % j);
if (rem == 0)
count = 1;
}
if (count == 0)
System.out.println("nmbr is prime" + i);
if (count == 1)
System.out.println("nmbr. is non-prime" + i);
}
}
public static void main(String[] args)
{
PrimeNoBtwnAnyTwoNo ob = new PrimeNoBtwnAnyTwoNo();
ob.prime_no_cll();
}
}
/** documentation comment
* 1st : check that you have to run loop from n1 to n2
* for prime number loop should run one less than the i
* caution : counter to be declare after for loop because we have to reset it every time
* if user enter larger number before for that apply if condition to swap the digit before only
*/

                 OUTPUT

Enter 1st no
2
Enter 2nd no
11
nmbr is prime2
nmbr is prime3
nmbr. is non-prime4
nmbr is prime5
nmbr. is non-prime6
nmbr is prime7
nmbr. is non-prime8
nmbr. is non-prime9
nmbr. is non-prime10
nmbr is prime11

No comments:

Post a Comment