KeeganT

Ch6Ex1b

Feb 21st, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. package ch6ex1b;
  2. import java.util.Scanner;
  3.  
  4. public class Ch6Ex1b
  5. {
  6.     public static void main(String[] args)
  7.     {
  8.         Scanner sc=new Scanner(System.in);
  9.         System.out.print("Enter a number: ");
  10.         int num=sc.nextInt();
  11.         System.out.print("Enter another number: ");
  12.         int num2=sc.nextInt();
  13.         boolean isPrime;
  14.         if(num2<num)
  15.         {
  16.             int temp=num2;
  17.             num2=num;
  18.             num=temp;
  19.         }
  20.         System.out.println("Prime numbers between "+num+" and "+num2+" are: ");
  21.         num+=1;
  22.         num2-=1;
  23.         for(int c=num;c<=num2;c++)
  24.         {
  25.             isPrime=true;
  26.             if(c<2)isPrime=false;
  27.             else for(int x=2;x<c;x++)if(c%x==0)isPrime=false;
  28.             if(isPrime)System.out.println(c);
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment