# Java Program to find prime number upto N number

Posted March 13, 2016

on:**Objectives :**

- Write a Java program to find prime number upto N number
- Write a Java program to print prime number upto N number
- Write a Java program to find prime number upto N number using Command Line Arguments
- Write a Java Program to find prime number upto N number via Command Line Arguments
- Write a Java program to find prime number upto N number using Scanner class

**Java Program / Code :**

**Method 1 : Java Program to find Prime Number upto N number using Scanner class**

import java.util.Scanner; class PrimeNumber { public static void main(String[] args) { int n,p; Scanner s=new Scanner(System.in); System.out.println(“Enter number : ”); n=s.nextInt(); for(int i=2;i<n;i++) { p=0; for(int j=2;j<i;j++) { if(i%j==0) p=1; } if(p==0){ System.out.println(i); } } } }

**Method 2 : Java Program to find Prime Number upto N number using Scanner class and writing function to find prime number**

Method isPrime() for checking if a number is prime or not

public class PrimeNumber{ public boolean isPrime(int num) { if ( num < 2 ){ return false; } for (int i = 2; i <= Math.sqrt(num); i++) { if ( num % i == 0 ) { return false; } } return true; } } // Mock Test Class to test above code import java.util.Scanner; public class Demo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter a number: "); int num = scanner.nextInt(); if ( num < 2 ) { System.out.println("\n There are no Prime Numbers available"); System.exit(0); } System.out.println("\n Prime Numbers from 1 to "+ num); PrimeNumber primeNum = new PrimeNumber(); for (int i = 3; i <= num; i++) { if ( primeNum.isPrime(i) ) { System.out.print(", " + i); } } } }

**Method 3 : Java Program to find Prime Number upto N number using Command line input**

class PrimeNumber { public static void main(String[] args) { int n,p; n=Integer.parseint(args[0]); for(int i=2;i<n;i++) { p=0; for(int j=2;j<i;j++) { if(i%j==0) p=1; } if(p==0){ System.out.println(i); } } } }

Steps to run above program via command line :

- Compilation : C:\JavaPrograms>javac PrimeNumber.java
- Interpretation : C:\JavaPrograms>java PrimeNumber 20

Output :

2

3

5

7

11

13

17

19

Advertisements

Tags: Aatul Palandurkar, AnCyber Pune, Atul Palandurkar, Best Java Trainer in Pune, Best Java Training in Pune, Best Java Tutorials, Core Java Tutorial, Core Java Tutorials, Core Java Workshop, Free Java Codes, Free Java Project, Free Java Source Code, Free Java Tutorials, Hibernate Training, Hibernate Training in Pune, HP assignments, HP Education Services, HP Internship Assignments, HP Summer Internship Assignments, HP Winter Internship Assignments, HPES, HPES assignments, HPES Internship Assignments, HPES Summer Internship Assignments, Java Assignment Questions, Java Assignments, Java Code for Prime Number, java program, Java Program for Beginners, Java program for Command Line Argument, Java Program for Prime number, java program for prime numbers upto n input via command line, java program for prime numbers upto N Number, Java Program for Scanner class, Java Program to find prime number upto N number, Java Program to find Prime Number upto N numbers, Java Program to find Prome Number, Java program to Find whether given number is Prime or Not, Java program to Find whether number is Prime or Not, Java Programming, Java Trainer, Java Training, Java Training in Pune, Java Tutorial, Java tutorial for beginners, Java Tutorials for Beginners, Java Workshop, Klouds AnCyber Technologies Pvt Ltd, Spring Training, Spring Training in Pune, Struts Training, Write a Java program to find prime number upto N number, Write a Java program to find prime number upto N number using Command Line Arguments, Write a Java Program to find prime number upto N number via Command Line Arguments, Write a Java program to print prime number upto N number

## Leave a Reply