Computer Science, asked by sangeetamondal1278, 5 months ago

write a program to accept a number from user and check whether the given number is a prime number or not​

Answers

Answered by anindyaadhikari13
4

Question:-

  • Write a program to check whether a number is prime or not.

Program:-

import java.util.*;

class Prime

{

static boolean isPrime(int n)

{

int c=0;

for(int i=1;i<=n;i++)

{

if(n%i==0)

c++;

}

return c==2;

}

public static void main(String args[])

{

int n, i;

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

n=sc.nextInt();

System.out.println((isPrime(n))?"Prime.":"Not Prime. ");

}

}

Answered by Oreki
1

import java.util.Scanner;

public class PrimeNumber {

static boolean isPrime(int number) {

for (int i = 2; i < number; i++)

if (number % i = = 0)

return false;

return true;

}

public static void main(String[ ] args) {

System.out.print("Enter a number - ");

int number = new Scanner(System.in).nextInt( );

System.out.printf("Number, %s a Prime", isPrime(number) ? "is" : "not");

}

}

Similar questions