prime number, c# prime number, prime number in c#, c# prime number ,program to find prime number by interview preparation
Program to find prime number by interview preparation

Welcome back, visitors. In this post, we will delve into the concept of prime numbers and the rationale behind prime number programs from an interview perspective. If you haven’t explored other pattern and pyramid programs, I highly recommend doing so. They offer fascinating insights into different algorithmic and mathematical concepts.


What is prime number

This interview question is frequently posed by interviewers seeking candidates with strong numerical and programming skills in mathematics.

A prime number is a positive integer that can only be divided by itself and 1 without leaving a remainder. For example, 11 is a prime number because it cannot be divided evenly by any other number apart from 1 and itself, resulting in a remainder of 0.


Logic behind prime number

Before delving into the logic, let’s first utilise your calculator to identify prime numbers. Open your calculator and enter a random number. Then, divide it by each integer from 1 to itself. If you encounter a remainder when dividing by any number other than 1 and itself, then the number is not a prime number.

I hope you were able to find those prime numbers. If not, don’t worry, I’m sharing a few of them: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97.


Program to validate is prime number or not

Step 1

First, let’s declare the necessary variables to initiate the initial program. I’ve added two variables: an integer data type variable to check if it’s a prime number or not, called “inputNum”, and a boolean data type variable to create conditional statements, called “isPrimeNumber”.


static void Main(string[] args)
{
    int inputNum = 11;
    bool isPrimeNumber = true;
}

Step 2

Secondly, as discussed in the above section, we need a loop that starts from 2 to the inputNum itself. This loop is responsible for checking if any of the numbers in between have the ability to return a remainder of 0.


static void Main(string[] args)
{
    int inputNum = 11;
    bool isPrimeNumber = true;

    for (int i = 2; i < inputNum; i++)
    {

    }
}

Step 3

Thirdly, let’s add a logic: if any of the numbers from our loop divide inputNum and return a remainder of 0, it implies that it’s not a prime number, and we should break the loop.


static void Main(string[] args)
{
    int inputNum = 11;
    bool isPrimeNumber = true;

    for (int i = 2; i < inputNum; i++)
    {
        if (inputNum % i==0)
        {
            isPrimeNumber = false;
            break;
        }
    }

}

Step 4

Lastly, let’s add a condition to validate the isPrimeNumber variable and display whether the number is prime or not.


static void Main(string[] args)
{
    int inputNum = 11;
    bool isPrimeNumber = true;

    for (int i = 2; i < inputNum; i++)
    {
        if (inputNum%i==0)
        {
            isPrimeNumber = false;
            break;
        }
    }

    if (isPrimeNumber)
    {
        Console.WriteLine($" {inputNum} is prime number ? YES");
    }
    else
    {
        Console.WriteLine($" {inputNum} is prime number ? NO");
    }
}

Summarizing the prime number program

The prime number program begins by initializing two variables: “inputNum”, an integer to hold the number being checked, and “isPrimeNumber”, a boolean indicating whether the number is prime. Next, a loop is established to iterate from 2 to “inputNum” inclusively. Within this loop, each number is tested to see if it evenly divides “inputNum”. If a divisor is found, “isPrimeNumber” is set to false, and the loop is terminated early. Following the loop, a condition assesses the value of “isPrimeNumber”. If true, the program concludes that “inputNum” is prime; otherwise, it determines the number is not prime. Finally, the result is displayed to indicate the prime status of “inputNum”.

Leave a Reply

Your email address will not be published. Required fields are marked *