Java Prime Number

  1. Find Prime Numbers in Java
  2. Check given number is Prime or Not in Java
  3. Find Prime Numbers between two numbers in Java
  4. Reverse order Prime Numbers in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Find Prime Numbers in Java


PrimeNumber.java

import java.io.BufferedReader;
import java.io.InputStreamReader;
 
public class PrimeNumber {
	
	public static void main (String args[]) {
		
		BufferedReader bf = new BufferedReader
				(new InputStreamReader(System.in));
		
		System.out.println("Enter n value :");
		
		int n = 0;
		int i, j;
		int PrimeCount = 0;
		
		try {
			
			n = Integer.parseInt(bf.readLine());
		} 
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("Prime numbers are : ");
		
		for(i=2; i<=n; i++) {
			
			int count = 0;
			
			for(j=1; j<=i; j++) {
								
				if((i%j) == 0) {
					
					count++;
				}
			}
			if(count == 2) {
				
				PrimeCount++;
				System.out.print("  " + i);
			}
		}
		
		System.out.print("\nTotally " + PrimeCount + " Prime numbers are available..");
	}
}

Sample Output

Enter n value :
30
Prime numbers are : 
  2  3  5  7  11  13  17  19  23  29
Totally 10 Prime numbers are available..




B. Check given number is Prime or Not in Java


CheckPrimeNumber.java

import java.io.BufferedReader;
import java.io.InputStreamReader;
 
public class CheckPrimeNumber {
	
	public static void main(String args[]) {
		
		BufferedReader bf = new BufferedReader
				(new InputStreamReader(System.in));
		
		System.out.println("Enter the Number to Check Prime or Not");
		int num = 0;
		
		try {
			
			num = Integer.parseInt(bf.readLine());
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		int count = 0;
		
		for(int i=1; i<=num; i++) {
						
			if((num%i) == 0) {
				
				count++;
			}			
		}
		if(count == 2) 
			System.out.println(num + " is a Prime Number..");
		else
			System.out.println(num + " is not a Prime Number..");
	}
}

Sample Output

Enter the Number to Check Prime or Not
97
97 is a Prime Number..




C. Find Prime Numbers between two numbers in Java


CheckPrimeBetween.java

import java.io.BufferedReader;
import java.io.InputStreamReader;
 
public class CheckPrimeBetween {
	
	public static void main(String args[]) {
		
		BufferedReader bf = new BufferedReader
				(new InputStreamReader(System.in));
		
		int start = 0, end =0;
		
		try {
			
			System.out.println("Enter Starting Number : ");
			start = Integer.parseInt(bf.readLine());
			
			System.out.println("Enter Ending Number : ");
			end = Integer.parseInt(bf.readLine());
		} 
		 catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		for(int i=start; i<=end; i++) {
			
			int count = 0;
			
			for(int j=1; j<=i; j++) {
								
				if((i%j) == 0) {
					
					count++;
				}
			}
			if(count == 2) {
				System.out.print("  " + i);
			}
		}
	}
}

Sample Output

Enter Starting Number : 
60
Enter Ending Number : 
80
  61  67  71  73  79




D. Reverse order Prime Numbers in Java


ReversePrime.java

import java.util.Scanner;
 
public class ReversePrime {
	
	public static void main(String args[]) {
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Enter n value :");
		
		int n = 0;
		int i, j;
		int PrimeCount = 0;
 
		n = Integer.parseInt(scan.nextLine());
	
		for(i=n; i>=2; i--) {
			
			int count = 0;
						
			for(j=1; j<=i; j++) {
								
				if((i%j) == 0) {
					
					count++;
				}
			}
			if(count == 2) {
				
				PrimeCount++;
				System.out.print("  " + i);
			}
		}
		
		System.out.print("\nTotally " + PrimeCount + " Prime numbers are available..");
	}
}

Sample Output

Enter n value :
25
  23  19  17  13  11  7  5  3  2
Totally 9 Prime numbers are available..






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com