C Program to check prime numbers | LearntHub

Hello friends, in this post today, we will learn that check whether entered number is prime or not in C programming language.
Before starting the program, we need to know what are prime numbers.
Prime numbers
Let's start to know that what are prime numbers.

Prime Numbers:-

A prime number is a type of numbers, it is a number which divides only by 1 and itself is called prime number.
For example: 1, 3, 5, 7, 13, 19.......
Friends, we have learned that what is prime numbers from the definition given above, and now we will  learn how we can check whether number is prime number or not from below program using decision making statement and loops.
  1. Operators
  2. Conditions Statements
  3. Loops
Let's know, how we can check whether number is prime in C programming.

Output 1:

Enter a number to check: 73
Enter number 73 is a prime.

Output 2:

Enter a number to check: 93
Enter number 93 is not a prime

To find prime number, we follow the following step, which showing in the above program.
1. First, we take preprocessor directive in the program.
For example: #include<stdio.h>
It's mean that this is Include with Standard Input Output Header File.
2. Next is, main function.
i.e., int main()
It's mean that this is Integer Main function.
3. Next is, variables declaration. In variables declaration, we decleare the variables, which define in the program.
4. Next step is printf(). printf() function uses for print(Output) to the statement on the monitor.
5. Next is, scanf() function. This function used for read(input) variables.
In this program %d uses for read the integer data type and & (address operator) is address of  variable.
6. In the next step, we use logic, 
First we use for loop, 
for(i=2;i<=n;i++) Let us taking n=4.
i.e, for(i=2;i<=3;i++)
Next is,
if(n%i==0)
i.e, if(3%2==0) so it is false
In the output, we find the given number is prime or not

Post a comment

3 Comments