Page Contents
Interview Content
- C Programming Coding Questions
- C Pattern Programming Questions
- C Programming Interview Questions
- Java Programming Coding Questions
- Java Pattern Programming Questions
- Java Programming Interview Questions
- Python Programming Coding Questions
- Python Pattern Programming Questions
- Python Programming Interview Questions
- SQL Interview Questions
Prime number program in java
Ans:
In this tutorial we are going to learn how to write a program to check whether a given integer number is prime number or not in Java programming language.
How our Java program will work?
Our Java program will take one integer number as input.
Suppose if someone gives an input 5 (As we know 5 is Prime number) then our program will give output “given number is a prime number”.
And if someone gives 8 then output will be “given number is not a prime number”.
Java program to check given number is prime or not
import java.util.*;
class Main{
public static void main(String ...a){
int i=0,temp=0;
Scanner sc= new Scanner(System.in);
System.out.print("Enter number- ");
int n= sc.nextInt();
for(i=2;i<=(n/2);i++){
if(n%i==0)
{
temp=1;
break;
}
}
if(temp==1)
System.out.println("Number is not a prime");
else
System.out.println("Number is prime");
}
}
Output:
Also prepare these Java Programs:
- Java Program to print Fibonacci series using recursive method
- Palindrome program in Java for number using Recursive method
- Java program to check a given is an Armstrong
- Java program to add two numbers without using addition operator
- How to remove a character from a string in java
- Write a program in Java to count occurrence of a given character in a String.
- Java program to find largest and smallest number in an array
- Java program to print first duplicate number in an array of 1-100
- Java Program to remove duplicate elements from an array
- Program to count repeated elements in an array java
Latest Uploads on Website
- AVL Tree with explanation
- Radix sort algorithm explanation with example
- Quick Sort Algorithm with explanation
- Bubble sorting algorithm with Bubble sort program in C
- Insertion sort algorithm and program in C
- Selection Sort Algorithm and Program in C
- Linear probing technique explanation with example
- Collision in Hashing and Collision resolution technique
- Hashing in data structure with its types
- Binary search tree operations with Program
- Binary search tree in data structure
- Binary search algorithm in data structure with explanation
- linear search in data structure with Algo and Program