Important alert: (current site time 6/19/2013 4:55:12 AM EDT) Brand new: recommended reading for every programming language on the site | revamped discussion board | 7 Source Code CDs

# Prime Number Program

Email
 Submitted on: 6/27/2012 8:56:52 AM By: Khalil Basha Level: Beginner User Rating: Unrated Compatibility: Java (JDK 1.5) Views: 1220

To print the prime numbers in given range.

code:
Can't Copy and Paste this?
 Terms of Agreement:    By using this code, you agree to the following terms...    You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.    You may link to this code from another website, but ONLY if it is not wrapped in a frame.  You will abide by any additional copyright restrictions which the author may have placed in the code or code's description. ``` //************************************** // Name: Prime Number Program // Description:To print the prime numbers in given range. // By: Khalil Basha // //This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=7037&lngWId=2//for details.//************************************** import java.util.Scanner; public class PrimeNumber1 { int i; void isPrime(int number){ for(int i = 2; i < number; i++){ if(number % i == 0){ System.out.println(number + "is not prime"); break; } else{ if(i == number){ System.out.println(number +"is prime"); } } } } } class PrimeDemo{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter Number to check Prime or Not: "); int num= sc.nextInt(); PrimeNumber p= new PrimeNumber1(); p.isPrime(num); } } //to print in given range public class PrimeNumber { private Exception Exception; void isPrime(int num){ int i; for ( i=2; i < num ;i++ ){ int n = num%i; if (n==0){ //System.out.println(num+"not Prime!"); break; } } if(i == num){ System.out.print(num+" "); } } void primerange(int min,int max)throws Exception{ try{ if(min>max){ throw Exception; } }catch(Exception e){ System.out.println("Range not defined"); } for(int j=min;j<=max;j++){ isPrime(j); } } } public class PrimeDemo { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); System.out.println("Enter Starting range: "); int min= sc.nextInt(); System.out.println("Enter Ending range: "); int max= sc.nextInt(); PrimeNumber p = new PrimeNumber(); p.primerange(min,max); } }```

Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)