C Program To Find Prime Number or Not using For Loop


Lets write a C program to check whether user input number is prime number or not, using for loop.

Prime Number: is a natural number greater than 1, which has no positive divisors other than 1 and itself.

Related Read:
For Loop In C Programming Language
if else statement in C
break Statement In C Programming Language

In this video tutorial we’re illustrating 3 methods to find if the user entered number is prime number or not.

For loop Logic

All the numbers are perfectly divisible by number 1, so we initialize the variable count to 2. So our c program starts checking for divisibility from number 2.

Video Tutorial: C Program To Find Prime Number or Not using For Loop


[youtube https://www.youtube.com/watch?v=FzHHB5EqNA0]

YouTube Link: https://www.youtube.com/watch?v=FzHHB5EqNA0 [Watch the Video In Full Screen.]

Method 1 Source Code: Prime Number or Not

#include<stdio.h>

int main()
{
    int num, count, prime = 1;

    printf("Enter a positive number\n");
    scanf("%d", &num);

    for(count = 2; count < num; count++)
    {
        if(num % count == 0)
        {
            prime = 0;
            break;
        }
    }

    if(prime)
        printf("%d is a Prime Number\n", num);
    else
        printf("%d is a not Prime Number\n", num);

    return 0;
}

Output 1:
Enter a number
7
7 is prime number

Output 2:
Enter a number
10
10 is not prime number

Logic: Method 1

We ask the user to enter a positive number and store it in variable num. Using for loop we start dividing the user entered number from 2 to num-1 times. If any number from 2 to num-1 perfectly divide the user entered number, then it’s not a prime number. We assign value 0 to variable prime and break out of the loop and print the message to the user.

Method 2 Source Code: Prime Number or Not: Divide By 2

#include<stdio.h>

int main()
{
    int num, count, prime = 1, inum;

    printf("Enter a positive number\n");
    scanf("%d", &num);

    inum = num / 2;

    for(count = 2; count <= inum; count++)
    {
        if(num % count == 0)
        {
            prime = 0;
            break;
        }
    }

    if(prime)
        printf("%d is a Prime Number\n", num);
    else
        printf("%d is a not Prime Number\n", num);

    return 0;
}

Output 1:
Enter a number
41
41 is prime number

Output 2:
Enter a number
15
15 is not prime number

Logic: Method 2

Please read the logic for method 1 above before proceeding.
In this method, we divide the user entered number by 2. This reduces the number of iterations of for loop.

If num = 41;
inum = num / 2;
inum = 41 / 2;
inum = 20;

So its enough if we iterate through the for loop 19(num/2) times to check if number 41 is perfectly divisible by any number from 2 to 20.

Method 3 Source Code: Prime Number or Not: square root Method

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

int main()
{
    int num, count, prime = 1, inum;

    printf("Enter a positive number\n");
    scanf("%d", &num);

    inum = sqrt(num);

    for(count = 2; count <= inum; count++)
    {
        if(num % count == 0)
        {
            prime = 0;
            break;
        }
    }

    if(prime)
        printf("%d is a Prime Number\n", num);
    else
        printf("%d is a not Prime Number\n", num);

    return 0;
}

Output 1:
Enter a number
50
50 is not prime number

Output 2:
Enter a number
53
53 is prime number

Logic: Method 3

Please read the logic for method 1 above before proceeding.
In this method, we apply square root to the user entered number and store it inside variable inum. This reduces the number of iterations of for loop even further.

If num = 41;
inum = sqrt(num);
inum = sqrt(41);
inum = 6;

So its enough if we iterate through the while loop 5( sqrt(num) ) times to check if number 41 is perfectly divisible by any number from 2 to 6.

Table of all prime numbers up to 1,000:

prime number or not

Note: We are not using curly braces around if and else because we only have 1 line of code after if and else – so curly braces are optional. If we have multiple lines of code, then we must use curly braces to wrap around the block of code.

You can also watch video for C Program To Find Prime Number or Not using While Loop

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

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