wap in java to check whether the no is prime or not
Answers
Answered by
0
Over here some syntax r missing but logic is correct
Attachments:
Answered by
1
- Write a program in Java to check whether a number is prime or not.
A number is said to be prime if it is divisible by 1 and itself.
So, we can say that the total number of factors is 2(1 and itself). In this program, we will check whether the total number of factors is 2 or not. If it is true then the number is prime else not.
Here is the code.
import java.util.*;
class Prime
{
public static void main(String s[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n=sc.nextInt();
int c=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
c++; // counting factors.
}
if(c==2)
System.out.println("Number is prime. ");
else
System.out.println("Number is not prime.");
}
}
Similar questions
English,
5 months ago
Social Sciences,
5 months ago
Science,
5 months ago
Geography,
10 months ago
Political Science,
10 months ago