Java Program to print Prime numbers in Java - Example Tutorial and Code

How to print Prime numbers in Java or how to check if a number is prime or not is classical Java programming questions, mostly taught in Java programming courses. A number is called prime number if its not divisible by any number other than 1 or itself and you can use this logic to check whether a number is prime or not. This program is slightly difficult than printing even or odd number which are relatively easier Java exercises. This Simple Java program print prime number starting from 1 to 100 or any specified number. It also has a method which checks if a number is prime or not.

This Java tutorial is in conjunction with my earlier tutorial for beginners like How to set Path in Java on windows and Unix , Java Program to reverse String in Java with recursion and recently how to read file in Java etc, if you haven’t read them you may find them useful.


Code Example to print Prime numbers in Java

Here is complete sample code example to print prime numbers from 1 to any specified number. This Java program can also check if a number is prime or not as prime number checking logic is encapsulated in isPrime(int number) method.

import java.util.Scanner ;

/**
* Simple Java program to print prime numbers from 1 to 100 or any number.
* A prime number is a number which is greater than 1 and divisible
* by either 1 or itself.
*/

public class
PrimeNumberExample {

public static void main ( String args []) {

//get input till which prime number to be printed
System. out. println ( "Enter the number till which prime number to be printed: " ) ;
int limit = new Scanner ( System. in ). nextInt () ;

//printing primer numbers till the limit ( 1 to 100)
System. out. println ( "Printing prime number from 1 to " + limit ) ;
for ( int number = 2 ; number <=limit ; number++ ){
//print prime numbers only
if (isPrime (number )){
System. out. println (number ) ;
}
}

}

/*
* Prime number is not divisible by any number other than 1 and itself
* @return true if number is prime
*/

public static boolean isPrime ( int number ){
for ( int i= 2 ; i <number ; i++ ){
if (number %i == 0 ){
return false ; //number is divisible so its not prime
}
}
return true ; //number is prime now
}
}

Output:
Enter the number till which prime number to be printed:
20
Printing prime number from 1 to 20
2
3
5
7
11
13
17
19


How to find if number is prime or not in JavaIn this Java program we have two part, first part which is taking input from user to print prime numbers and other part is function isPrime(int number) which checks whether a number is prime or not. Java method isPrime(int number) can also be used anywhere in code because its encapsulated logic of checking prime number. There is also another popular Java question is " How to check if a number is prime or not?", isPrime() can be used there. its rather simple and just implement the logic of prime number in Java i.e. divides a number, starting from 2 till the number itself, if its divisible by another number means its not prime.

That's all on how to print prime numbers in Java or how to check if a number is prime or not. Its worth remembering logic that when a number is prime and how do you check for prime number. If you are doing homework then get an Idea but type the program yourself , that will give you thinking time on how this Java program is working and checking for prime numbers.

Related Java Program tutorials



Read more: http://javarevisited.blogspot.com/2012/04/java-program-to-print-prime-numbers-in.html#ixzz2khcHnL1c
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值