Java Program
Write a program to input a number & check whether it is a prime number or not
Answers
Answered by
5
public class PrimeNumbers {
public void prime(int n) {
boolean isPrime = true;
for (int i = 2; i < n; i++) {
if (n % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.println(n + " is a prime number"); } else {
System.out.println(n + " is not a prime number");
}
}
}
public void prime(int n) {
boolean isPrime = true;
for (int i = 2; i < n; i++) {
if (n % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.println(n + " is a prime number"); } else {
System.out.println(n + " is not a prime number");
}
}
}
Anonymous:
@sweety sorry u have answered in C++
Answered by
2
Answer:
import java.util.*;
class Prime_no
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of elements to be entered");
int n=sc.nextInt();
int a[]=new int[n];
int f;
System.out.println("Enter "+n+" elements");
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
System.out.println("Prime numbers:");
for(int i=0;i<n;i++)
{
f=0;
for(int j=1;j<=a[i];j++)
{
if(a[i]%j==0)
f++;
}
if(f==2)
System.out.println(a[i]);
}
}
}
Similar questions