Important alert: (current site time 6/19/2013 3:33:28 PM EDT)
 

VB icon

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: 1223
(About the author)
 
     To print the prime numbers in given range.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. 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.
  2. 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.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. 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);
 
 }
}


Report Bad Submission
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:

Your Vote

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 ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.