Write a program to input a number and check whether it is a prime number or not
Answers
Just a mere doubt in which language do you want the program to be in. Assuming that is JavaScript, here's the program.
public class PrimenumberToCheckCheck {
public static void main(String[] args) {
int remainder;
boolean isPrime=true;
int numberToCheck=17; // Enter the numberToCheckber you want to check for prime
//Loop to check whether the numberToCheckber is divisible any numberToCheckber other than 1 and iteself
for(int i=2;i<=numberToCheck/2;i++)
{
//numberToCheckber is dived by itself
remainder=numberToCheck%i;
System.out.println(numberToCheck+" Divided by "+ i + " gives a remainder "+remainder);
//if remainder is 0 than numberToCheckber is not prime and break loop. Elese continue loop
if(remainder==0)
{
isPrime=false;
break;
}
}
// Check value true or false,if isprime is true then numberToCheckber is prime otherwise not prime
if(isPrime)
System.out.println(numberToCheck + " is a Prime numberToCheckber");
else
System.out.println(numberToCheck + " is not a Prime numberToCheckber");
}
}
Answer:
import java.util.*;
public class Number
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int a,n,c;
System.out.println("Enter the number");
a=in.nextInt();
for(n=2;n=a/2;n++)
{
c=(a%n);
if(c==0)
{
System.out.println("It is a composite number");
else
System.out.println("It is a prime number");
}
}
}
}
Explanation:
● A prime number is that which is divisible by itself and by 1.
● As the program is reparative so we should use loop here such as for loop.
● A number whether it is prime or composite but never gets divided by number which is more than its half.0