1.

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

Answer»

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



Discussion

No Comment Found