Write a C program for prime number check [So Easy]

C programming is easy if you know the basic concepts and syntax of programming. Today, we understand C program for prime number.

So, Let’s get started.

C program for prime number

C program for prime number

void main()
int n,i,flag=0;
printf("Enter any number to check prime or not");

printf("Number is Prime");
printf("Number is Odd");

Program Code Explanation (PCE)

Hope that you understand C program for prime number code; but if you do not understand, I am explaining above code here.

We need one variable (n) to input value from user to check for prime or not. one another variable for counter (i.e. i for checking each value by dividing from 2 to n-1 or n/2).

We have a resultant variable called ‘flag’. It is used to check the number (n) after each counter pass (i). if any value of ‘i’ divides ‘n’ completely, we change the status of flag variable from 0 to 1.

As you can see in the code, we initialised flag variable by zero. So after the execution of code, it remains zero means number that entered by the user is prime otherwise it is not prime.

See Also

Hope that you understand the above C program for prime number code. If you have any questions please ask in below comment section. Don’t forget to share it in your social family and friends.

You can also visit our tech blog earningcrows.com

Leave a Comment

error: Don\'t make piracy - Write your own content :)