codescracker
c

C Program to Check Prime Number or Not



« Previous Program Next Program »

In this tutorial, we will learn about how to create a program in C that will check whether the given number (by user at run-time) is a Prime number or not. We will also learn how to print all prime and composite numbers present in between any two number say 1 and 20, that is all prime and composite number between 1 to 20.

At last, we have created a program that will ask from user to enter any two number to display all the prime and composite numbers present in between the given two number. Let's first take a look at the program that will check whether the number entered by user is a prime or not.

How to Check for Prime Number ?

If number is divisible by any number from 2 to one less than the number (i.e. n-1). Let's suppose that user has supplied a number say 37 as input, then if 37 is divisible by any number from 2 to 36, then the number is not a prime number, otherwise it will be a prime number.

Note - Prime number is a number that can not be divided by any number except 1 and the number itself.

Check Prime Number or Not in C

To check whether the input number is a prime number or not in C programming, you have to ask from user to enter a number and start checking for prime number as shown in the program given below.

#include<stdio.h>
#include<conio.h>
int main()
{
    int num, i, count=0;
    printf("Enter a number: ");
    scanf("%d", &num);
    for(i=2; i<num; i++)
    {
        if(num%i == 0)
        {
            count++;
            break;
        }
    }
    if(count==0)
        printf("\nIt's a prime number");
    else
        printf("\nIt's not a prime number");
    getch();
    return 0;
}

As the above program was written under Code::Blocks IDE, therefore after successful build and run, here is the sample. This is the first snapshot of the sample run:

c program to check prime or not

Supply any number say 37 and press ENTER key to check whether it is a Prime number or not. Here is the second snapshot of the sample run:

prime number program c

Program Explained

Print all Prime & Composite Numbers

Now let's create a program that will find and print all prime and composite numbers present in between 1 to 20:

#include<stdio.h>
#include<conio.h>
int main()
{
    int i, j, count=0;
    printf("\t\tBetween 1 to 20:\n\n");
    printf("Prime Numbers\t\tComposite Numbers\n");
    for(i=1; i<=20; i++)
    {
        for(j=2; j<i; j++)
        {
            if(i%j == 0)
            {
                count++;
		break;
            }
        }
        if(count==0)
            printf("%d", i);
        else
            printf("\t\t\t%d", i);
        count=0;
        printf("\n");
    }
    getch();
    return 0;
}

After successful build and run, here is the sample run. This is the first screenshot:

c program print prime composite number

Program Explained

Let's create the same program but this time the program given below displays prime number in one line and composite number in second line. This program will not display the prime and composite number in table form. Here is the program:

#include<stdio.h>
#include<conio.h>
int main()
{
    int i, j, count=0, arr[20], k=0;
    printf("\t\tBetween 1 to 20:\n\n");
    printf("Prime Numbers: ");
    for(i=1; i<=20; i++)
    {
        for(j=2; j<i; j++)
        {
            if(i%j == 0)
            {
                count++;
		break;
            }
        }
        if(count==0)
            printf("%d ", i);
        else
        {
            arr[k] = i;
            k++;
        }
        count=0;
    }
    printf("\nComposite Numbers: ");
    for(i=0; i<k; i++)
        printf("%d ", arr[i]);
    getch();
    return 0;
}

Here is the sample:

print all prime composite number c

In the above program, we have checked whether the given number is a prime number or not as told in above steps, if it is, then print it out, otherwise initialize the current value to any array. This array will hold up its element as all the composite numbers present in between 1 and 20.

Allow User to Define Range

The question is, Write a program in C to print all composite and prime numbers between given range:

#include<stdio.h>
#include<conio.h>
int main()
{
    int i, j, count=0, arr[100], k=0, rangeStart, rangeEnd;
    printf("Enter Starting Number: ");
    scanf("%d", &rangeStart);
    printf("Enter Ending Number: ");
    scanf("%d", &rangeEnd);
    printf("\t\tBetween %d to %d:\n\n", rangeStart, rangeEnd);
    printf("Prime Numbers: ");
    for(i=rangeStart; i<=rangeEnd; i++)
    {
        for(j=2; j<i; j++)
        {
            if(i%j == 0)
            {
                count++;
		break;
            }
        }
        if(count==0)
            printf("%d ", i);
        else
        {
            arr[k] = i;
            k++;
        }
        count=0;
    }
    printf("\nComposite Numbers: ");
    for(i=0; i<k; i++)
        printf("%d ", arr[i]);
    getch();
    return 0;
}

The snapshot given below is the sample run of above program:

find all prime composite number c

Now enter the range. Let's suppose, user has supplied 10 as starting number and 40 as ending number. Therefore here is the output you will see on your output screen after pressing ENTER key:

print all prime composite numbers c program

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker