by Dinesh Thakur Category: Control Structures

Suppose, the value entered is say 121. It is assigned to an integer variable num. Now, num is divided by all the numbers from 2 to 8 (because a number can be divided by 1 and itself) (% rehrrns remainder). If the number is divided by any number from 2 to 8, its remainder will come to be 0 and in that case the message "Number is not prime" is displayed on the screen. 121 is not divisible by 2 but is no doubt divisible by 3 which returns remainder as 0 and hence it is not a prime number.

**Algorithm For Prime Number Program:**

step 1: Read num

step 2: Set b=l, c=0

step 3: Repeat through step-5 while (b <= num)

step 4: If (num mod b) equals to 0 then set c=c+ 1

step 5: b=b+l

step 6: If c equals to 2 then print “num is prime”

Else print “num is not prime”

step 7: Exit

**Here is the Java Example for ****Prime Number Program:**

import java.util.Scanner;

public class PrimeNumber

{

public static void main(String args[])

{

int num,b,c;

Scanner s=new Scanner(System.in);

System.out.println("Enter A Number");

num =s.nextInt();

b=1;

c=0;

while(b<= num)

{

if((num%b)==0)

c=c+1;

b++;

}

if(c==2)

System.out.println(num +" is a prime number");

else

System.out.println(num +" is not a prime number");

}

}

About Dinesh Thakur

Dinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Search Content

Popular Article

#### Control statements in java

#### Difference between final, finally and finalize().

#### Strong Number Program in Java Example

#### Jump Statements in Java Example

#### Sum of First N Natural Numbers in Java Example

#### Prime Number Program in Java Using Scanner Example.

#### Java Program to Find Average of N Numbers

#### Perfect Number Program in Java Example.

#### Prime Number Program in Java Using BufferedReader Example.

#### Remove Vowels from a Sentence | Java Example

#### Check Number is Binary or Not Using Java Example

#### Fibonacci Series Using Recursion in Java Example

#### Armstrong Number With in Range Java Example

#### GCD of Two Numbers in Java Using While

#### Java Program Vowel or Not Example

#### Print Number from 1 to 10 Except 5

#### Prime Numbers Between Range Java Example

#### Odd Numbers from 1 To 100 in Java Example

#### Write a Java program for triangle of asterisks with maximum length of user input

#### Switch Statement in Java Example

#### Java Calculate The Sum Of Only The Positive Numbers. Example

#### Sum of Digits of a Number in Java Example

#### Java Nested For Loop Examples

#### Factorial of the Given Number in Java Example

#### Fibonacci Series in Java Example

#### Right Angled Triangle in Java Example

#### Floyd Triangle in Java Example

#### Reverse the Order of the Words in a Sentence in Java Example

#### Nested Switch Statements Java Example

#### Solve Equation 3*x*x - 2*x +1 in Java Example

#### If Else If Ladder, if..else ....if else statement in Java Example

#### Palindrome Number in Java Example.

#### Highest Prime Number Within the Given Range Java Example.

#### Find Recursion Power(x,n) Using Java Example

#### Enter a String from keyboard and check whether it Palindrome or Not

#### Comma Operator in Java Example

#### Print all the Prime Numbers up to 100

#### Java Example to translate the word in piglatin

#### Do-while Loop in Java, do while syntax Java Example

#### Nested If in Java Example

#### Print n Even Numbers in Java Example

#### How Many Digits Are Present in the Number - Java Example

#### Armstrong Number in Java Example

#### Enter 10 Number and Print the Number of Positive, Negative and Zeros Number

#### Math.random() in Java Example

#### currentTimeMillis() Java Example

#### Swap Two Variables Using XOR Operator Using Java Example

#### Program To Show Importance of Continue Statement? Java Example

#### Print System Date and Time in Java Example

#### Triangle of any size in Java Example

#### For Loop | Java Examples

#### While Loop in Java Example

#### Counting the Number of Non Blank Characters in a String | Java Example

#### Nested For Loop in Java Example

#### Date Format Example in Java

#### Print the table of an input number up till 10 times in Java Example

#### Java console io Example

#### Swap Variables in a Different Way Without Using Third Variables

#### Java - Number of Times a Word has Been Mentioned | Java Example

#### Java Sum of Digits – Java Example

#### Sum of first n Sequence Numbers in Java Example

#### Print n Sequence Numbers in Java Example

#### Java If Statement | Java Example

#### Print Tables from 5 to 10 Except 6

#### Infinite For loop Example | Java Examples

#### Finalizer methods in Java

#### How to calculate the GCD (Greatest Common Divisor) in Java

Basic Courses

Advance Courses