Prime Number Program | Check if Number is Prime

Prime Number Program, Are you interested in coding and want to tackle the challenge of identifying prime numbers? You’re in for an exciting journey! In this detailed guide, we’ll walk you through the process of developing a program that can determine whether a given number is prime or not. We’ll cover the entire process step by step using widely used programming languages like C, C++, Java, JavaScript, and Python. By the end of this guide, you’ll not only be able to determine prime numbers but also understand the fundamental logic behind them.

Prime Number Program

Certainly, let’s follow the 6-step strategy to create a program that determines whether a given number is prime or not in C, C++, Java, JavaScript, and Python.

Step 1: Understand the Problem:

The task is to create a code that can determine whether a given number is prime or not.

Step 2: Design Test Cases

Let’s design a few test cases to validate the accuracy of our programs:

Input: 5
Expected Output: Prime

Input: 10
Expected Output: Not Prime

Input: 17
Expected Output: Prime

Step 3: Derive the Solution (Pseudocode)

Next, we will derive the solution using pseudocode to understand the logic without worrying about the specific syntax of the programming language

FUNCTION isPrime(number)
    IF number <= 1
        RETURN "Not Prime"

    FOR i = 2 TO squareRoot(number)
        IF number MOD i EQUALS 0
            RETURN "Not Prime"

    RETURN "Prime"
END FUNCTION

Step 4: Test the Solution

Before writing the actual code, let’s perform a dry run of the pseudocode with our test cases to ensure it works as expected.

number = 5

5 > 1, so proceed

2 <=5, 5 % 20

Return "Prime"

Step 5: Write the Code (Java)

Now that we have a clear understanding of the logic, we can write the code in our preferred programming language.

Prime Number Program | C, C++, Java, JavaScript, Python

C
#include <stdio.h>
#include <math.h>

const char* isPrime(int number) {
    if (number <= 1) {
        return "Not Prime";
    }
    
    for (int i = 2; i * i <= number; i++) {
        if (number % i == 0) {
            return "Not Prime";
        }
    }
    
    return "Prime";
}

int main() {
    int number;
    
    printf("Enter a number: ");
    scanf("%d", &number);

    const char* result = isPrime(number);

    printf("The number is: %s\n", result);

    return 0;
}
C

C++
#include <iostream>
#include <cmath>
using namespace std;

const char* isPrime(int number) {
    if (number <= 1) {
        return "Not Prime";
    }
    
    for (int i = 2; i * i <= number; i++) {
        if (number % i == 0) {
            return "Not Prime";
        }
    }
    
    return "Prime";
}

int main() {
    int number;
    
    cout << "Enter a number: ";
    cin >> number;

    const char* result = isPrime(number);

    cout << "The number is: " << result << endl;

    return 0;
}
C++

Java
import java.util.Scanner;

public class PrimeNumberChecker {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int number = scanner.nextInt();

        String result = isPrime(number);

        System.out.println("The number is: " + result);

        scanner.close();
    }

    public static String isPrime(int number) {
        if (number <= 1) {
            return "Not Prime";
        }
        
        for (int i = 2; i * i <= number; i++) {
            if (number % i == 0) {
                return "Not Prime";
            }
        }
        
        return "Prime";
    }
}
Java

JavaScript
function isPrime(number) {
    if (number <= 1) {
        return "Not Prime";
    }
    
    for (let i = 2; i * i <= number; i++) {
        if (number % i === 0) {
            return "Not Prime";
        }
    }
    
    return "Prime";
}

const readline = require('readline').createInterface({
    input: process.stdin,
    output: process.stdout
});

readline.question('Enter a number: ', input => {
    const number = parseInt(input);
    const result = isPrime(number);
    console.log(`The number is: ${result}`);
    readline.close();
});

JavaScript

Python
import math

def is_prime(number):
    if number <= 1:
        return "Not Prime"
    
    for i in range(2, math.isqrt(number) + 1):
        if number % i == 0:
            return "Not Prime"
    
    return "Prime"

number = int(input("Enter a number: "))
result = is_prime(number)
print("The number is:", result)
Python

Step 6: Test the Code

Compile and run the Java program. Test it with the provided test cases to ensure it produces the correct output.

Prime Number Program | Conclusion

Congratulations! You’ve successfully mastered the art of creating programs to determine whether a number is a prime number or not in multiple programming languages. With this newfound knowledge, you’re well on your way to building a strong foundation in coding and problem-solving.



Leave a Comment