Computer Science, asked by moulikrajdeep, 8 months ago

wap in java to check whether the no is prime or not

Answers

Answered by soham2591
0
Over here some syntax r missing but logic is correct
Attachments:
Answered by anindyaadhikari13
1

\star\:\:\:\sf\large\underline\blue{Question:-}

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

\star\:\:\:\sf\large\underline\blue{Code:-}

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