Computer Science, asked by mehuleedas, 8 months ago

Write the Java Programming:- Input a number and check whether the number is prime or not.​

Answers

Answered by ArpitMishra506
4

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

Answered by anindyaadhikari13
3

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

  • Write a program in java to check whether the inputted number is prime or not.

\star\:\:\:\bf\large\underline\blue{Approach:-}

  • There are many approaches for this program. I am showing you one of them.

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. ");

}

}

\star\:\:\:\bf\large\underline\blue{Explanation:-}

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.

\star\:\:\:\bf\large\underline\blue{Similar\:Questions:-}

  • Write a program in Java to check whether a number is Disarium or not.
  • Write a program in Java to check whether a number id Armstrong number or not.
  • Write a program in Java to display the Fibonacci series upto N terms by taking input.
Similar questions