Computer Science, asked by rahul6xyz, 1 year ago

write a program in Java to print the given number is prime or not​

Answers

Answered by BrainlyProgrammer
2

Question:-

  • To check if the given number is a prime number or not.

_______

Code:-

package Coder;

import java.util.*;

public class PrimeCheck {

public static void main(String[] args) {

Scanner sc=new Scanner (System.in);

System.out.println("Enter a number");

int n=sc.nextInt();

int c=0;

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

{

if(n%i==0)

c++;

}

if(c==2)

System.out.println("Prime number");

else

System.out.println("Not a prime number");

}

}

__________

Variable Description:-

  1. n:- to accept the number from the user
  2. i:- loop variable
  3. c:- count variable to count the no. of factors of a number

_________

Output Attached

Attachments:
Similar questions