Prime Number Program in Java | Write a program to print Prime Number Program in Java.

Prime Number Program in Java

write a program to print Prime Number Program in Java.

import java.util.Scanner;

public class Prime
{
public static void main(String args[])
{
int num,i,count=0;
System.out.println("Enter a number: ");
Scanner enter = new Scanner(System.in);
num = enter.nextInt();
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1){
System.out.println(num + " is a prime number");
}
else{
System.out.println(num + " is not a prime number");
}
}
}

Output….

Enter a number:
9
9 is a prime number
Previous articlewrite a program to print the Fibonacci series in c. |Fibonacci series program in c.
Next articlePython: Split a string on the last occurrence of the delimiter| split string last occurrence python

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here