Write the Java Programming:- Input a number and check whether the number is prime or not.
Share
Write the Java Programming:- Input a number and check whether the number is prime or not.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Verified answer
public class Prime
{
public static void main(int num)
{
int f=0;
for(int a=1; a<=(num/2); a++)
{
if(num%a==0){
f=1;
break;
}
}
if(f==0)
System.out.println("Prime number") ;
else
System.out.println("Not a Prime number") ;
}
}
.
.
.
HOPE THIS HELPS YOU
.
.
.
.
PLEASE MARK AS BRAINLIEST AND FOLLOW ME TOO
[tex]\star\:\:\:\bf\large\underline\blue{Question:-}[/tex]
[tex]\star\:\:\:\bf\large\underline\blue{Approach:-}[/tex]
class IsPrime
{
public static void main(int number)
{
int c=0;
for(int i=1;i<=number;i++)
{
if(number%i==0)
c++;
}
if(c==2)
System.out.println("Number is prime. ");
else
System.out.println("Number is not prime. ");
}
}
[tex]\star\:\:\:\bf\large\underline\blue{Explanation:-}[/tex]
A number is said to be prime if the number is divisible only by 1 and itself. So, we can say that total number of factors of s prime number is always 2 i.e., 1 and itself. In this program, I have checked whether number of factors for the given number is 2 or not. If it is true, then it is prime and we will print "Number is Prime" else not.
[tex]\star\:\:\:\bf\large\underline\blue{Similar\:Questions:-}[/tex]