Sure, I can provide you with examples of how to check for prime numbers in various programming languages. Let's go through each of them with detailed explanations:
1. **C:**
```c
#include <stdio.h>
int isPrime(int num) {
if (num <= 1)
return 0;
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0)
return 0;
}
return 1;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isPrime(num))
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);
return 0;
}
```
**Explanation:** In the C program, the `isPrime` function checks if a number is prime by iterating from 2 to the square root of the number. If the number is divisible by any value within this range, it's not prime. Otherwise, it's prime.
2. **C++:**
```cpp
#include <iostream>
using namespace std;
bool isPrime(int num) {
if (num <= 1)
return false;
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0)
return false;
}
return true;
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (isPrime(num))
cout << num << " is a prime number." << endl;
else
cout << num << " is not a prime number." << endl;
return 0;
}
```
**Explanation:** The C++ program is similar to the C program, but it uses `cout` and `cin` for input and output.
3. **Java:**
```java
import java.util.Scanner;
public class PrimeCheck {
public static boolean isPrime(int num) {
if (num <= 1)
return false;
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0)
return false;
}
return true;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (isPrime(num))
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}
```
**Explanation:** The Java program is structured similarly to the C++ program, using `Scanner` for input and `System.out.println` for output.
4. **Python:**
```python
def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
num = int(input("Enter a number: "))
if is_prime(num):
print(f"{num} is a prime number.")
else:
print(f"{num} is not a prime number.")
```
**Explanation:** In the Python program, the `is_prime` function uses a similar logic as the other programs. It iterates up to the square root of the number and checks for divisibility.
5. **JavaScript:**
```javascript
function isPrime(num) {
if (num <= 1)
return false;
for (let i = 2; i * i <= num; ++i) {
if (num % i === 0)
return false;
}
return true;
}
const num = parseInt(prompt("Enter a number: "));
if (isPrime(num))
console.log(`${num} is a prime number.`);
else
console.log(`${num} is not a prime number.`);
```
**Explanation:** The JavaScript program follows a similar structure as the other examples. It uses `prompt` for input and `console.log` for output.
6. **PHP:**
```php
<?php
function isPrime($num) {
if ($num <= 1)
return false;
for ($i = 2; $i * $i <= $num; ++$i) {
if ($num % $i == 0)
return false;
}
return true;
}
$num = readline("Enter a number: ");
if (isPrime($num))
echo $num . " is a prime number.";
else
echo $num . " is not a prime number.";
?>
```
**Explanation:** The PHP program uses the `readline` function for input and `echo` for output. The `isPrime` function follows the same logic as the other examples.
Each of these programs uses a basic primality testing algorithm. They iterate through possible divisors up to the square root of the number and check for divisibility. If no divisors are found within that range, the number is considered prime.
Remember that these examples focus on demonstrating the concept of checking for prime numbers in various programming languages. In practice, there are more efficient algorithms for prime checking, especially when dealing with larger numbers.