write a program in Java to print the given number is prime or not
Answers
Answered by
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:-
- n:- to accept the number from the user
- i:- loop variable
- c:- count variable to count the no. of factors of a number
_________
•Output Attached
Attachments:
Similar questions
Science,
6 months ago
Business Studies,
6 months ago
English,
1 year ago
Math,
1 year ago
Biology,
1 year ago