Write a program to find whether a given number is prime or not.



Write a program to find whether a given number is prime or not...

Answer / Rahul Mishra

In Java, here's a simple program that checks if a number is prime:
```
import java.util.Scanner;
public class PrimeChecker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number to check for primality:");
int num = sc.nextInt();
if (num <= 1) {
System.out.println(num + " is not prime.");
} else {
for (int i = 2; i < num; i++) {
if (num % i == 0) {
System.out.println(num + " is not prime.");
return;
}
}
System.out.println(num + " is prime.");
}
}
}
```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Programming Languages AllOther Interview Questions

what is runtime exception and compiletime exception ?

1 Answers   Accenture,


DIFFERENCE BETWEEN BINDING LANG AND BIND DIR WHEN USING IN SERVICE PGM

0 Answers   CTS,


What is the merger sort principle and its time complexity.

1 Answers   Verifone,


What is the Difference between in memory database and physical database

1 Answers  


Can anyone provide as400 training material or any institute in bangalore for as400 training

10 Answers  


There are 3 jars containing two types of round marbles. One jar contains only red marbles, one jar contains only blue marbles and the third jar contains a mix of both red and blue marbles. Although the jars are labeled “red”, “blue” and “mixed” – all the jars are mislabeled. How many marbles would you have to pull out, and out of which jars, to find out how to fix the labels correctly?

1 Answers   Syntel,


what is the pl/sql block size in oracle 10g and 11g?

0 Answers  


what is the abap/4

1 Answers  


what is the current salary package in India for a lamp programmer

1 Answers   HCL,


iam confused among testing ,.net and java. can anybody help me.

1 Answers  


Hi guys... I have one doubt ..Exception is a runtime error then why we have checked exception... Thanks in advance

2 Answers  


What Is The Difeerence Between C & C++

2 Answers   HCL,


Categories