- C Programming Examples
- C Programming Examples
- C Print Hello World
- C Get Input from User
- C Print Integer
- C Add Two Numbers
- C Add Subtract Multiply Divide
- C Add n Numbers
- C Area Perimeter of Square
- C Area Perimeter of Rectangle
- C Area Circum of Circle
- C Fahrenheit to Celsius
- C Celsius to Fahrenheit
- C Inches to Centimeters
- C Kilogram to Gram
- C Reverse a Number
- C Swap Two Numbers
- C Interchange Numbers
- C Print ASCII Value
- C Print Fibonacci Series
- C Check Palindrome or Not
- C Check Armstrong or Not
- C Find Armstrong Numbers
- C Find nCr and nPr
- C Find Profit Loss
- C Sum of their Square
- C First & Last Digit Sum
- C Sum of All Digit
- C Product of All Digit
- C Print Total Digit in Number
- C Check Perfect Number
- C Find Basic Gross Salary
- C Round Number to Integer
- C Print Series upto n Term
- C Find Factors of Number
- C if-else & Loop Programs
- C Check Even or Odd
- C Check Prime or Not
- C Check Alphabet or Not
- C Check Vowel or Not
- C Check Leap Year or Not
- C Is Reverse Equal Original
- C Make Calculator
- C Add Digits of Number
- Count Positive Negative Zero
- C Largest of Two Numbers
- C Largest of Three Numbers
- C Smallest of Two Numbers
- C Smallest of Three Numbers
- C Find Factorial of Number
- C Find LCM & HCF
- C Find LCM of n Numbers
- C Find HCF of n Numbers
- C Find Arithmetic Mean
- C Find Average, Percentage
- C Find Student Grade
- C Print Table of Number
- C Print Prime Numbers
- C Find Discount Purchase
- C Calculate Parcel Charge
- C Calculate Wage of Labor
- C Print Phone Bill
- C Conversion programs
- C Decimal to Binary
- C Decimal to Octal
- C Decimal to Hexadecimal
- C Binary to Decimal
- C Binary to Octal
- C Binary to Hexadecimal
- C Octal to Decimal
- C Octal to Binary
- C Octal to Hexadecimal
- C Hexadecimal to Decimal
- C Hexadecimal to Binary
- C Hexadecimal to Octal
- C Pattern Programs
- C Pattern Printing Programs
- C Print Diamond Pattern
- C Print Floyd's Triangle
- C Print Pascal's Triangle
- C Array Programs
- C 1D Array Programs
- C Linear Search
- C Binary Search
- C Largest Element in Array
- C Smallest Element in Array
- C Second Largest/Smallest
- C Count Even Odd
- C Array Element at Even
- C Array Element at Odd
- C Print Even Array Elements
- C Print Odd Array Elements
- C Sum/Product of Even/Odd
- C Reverse an Array
- C Insert Element in Array
- C Delete Element from Array
- C Merge Two Arrays
- C Bubble Sort
- C Selection Sort
- C Insertion Sort
- C Print Common Elements
- C 2D Array Programs
- C Add Two Matrices
- C Subtract Two Matrices
- C Transpose a Matrix
- C Multiply Two Matrices
- C Sum All Matrix Elements
- C Largest Element in Matrix
- C Print Row Column Total
- C 3D Array Programs
- C String Programs
- C Print String
- C Find Length of String
- C Compare Two String
- C Copy a String
- C Concatenate String
- C Reverse a String
- C Count Vowels Consonants
- C Replace Vowel in String
- C Delete Vowels from String
- C Delete Word from String
- C Frequency of Character
- C Count Word in String
- C Remove Spaces from String
- C Sort a String
- C Sort String in Alphabetical
- C Sort Words in Ascending
- C Sort Words in Descending
- C Uppercase to Lowercase
- C Lowercase to Uppercase
- C Swap Two Strings
- C Check Anagram or Not
- C Check Palindrome String
- C Print Number in Words
- C Print Successive Character
- C Character without Space
- C Remove Extra Spaces
- C File Programs
- C Read a File
- C Write Content to File
- C Read & Display File
- C Copy a File
- C Merge Two Files
- C Reverse File
- C Count All Character in File
- C List Files in Directory
- C Encrypt & Decrypt a File
- C Delete a File
- C Misc Programs
- Generate Random Numbers
- C Print Date Time
- C Print Message with Time
- C Get IP Address
- C Print Smiling face
- C Pass Array to Function
- Add Two Numbers using Pointer
- C Address of Variable
- C Shutdown Computer
- C Programming Tutorial
- C Tutorial
- C Programming Library
- C Standard Library
- C Programming Test
- C Programming Test
- Give Online Test
- All Test List

# C Program to Check Prime Number or Not

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:

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:

#### Program Explained

- Receive any number as input say 37
- Create a for loop that runs from 2 to one less than the number, that is
**37-1**or**36** - Inside the
**for**loop, check whether current value of loop variable say**i**divides the number say**37**(given by user at run-time) without leaving any remainder or not - If it divides, then program flow goes inside the if block, and increment the value
of any variable say
**count**here and then use**break**keyword to exit from the loop. As if any number except 1 and the number itself, divides the number means the number is not a prime number - Never forgot to initialize 0 to the
**count**variable as its initial value at start of the program - Now after exiting from the loop whether using break keyword or normally, check whether
**count**variable holds its original value or not, if it holds (that is 0), it means the program flow never goes inside the**if**block, and it means any number from 2 to 36 does not completely divides the number, therefore the given number is a prime number - Otherwise, if
**count**variable holds the value other than 0, then it means that the given number is not a prime number

### 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:

#### Program Explained

- Create a
**for**loop that runs from 1 to 20 - For each and every value of loop variable say
**i**(1, 2, 3, 4, ..., 20), check whether it is a prime or not - Here we have create a two column, first is for prime and second is for composite numbers
- If the number is found as prime number, then print it without adding any horizontal tab (\t)
- And if the number is found as composite number, then print it after two or three horizontal tab to show it in second column
- Now come to the point, inside the
**for**loop, we have created another**for**loop with another loop variable say**j**, here we have run the loop from 2 to one less than the outer loop variable's current value (i-1). That is, if the current value of**i**is 17, then check whether any number from 2 to 16 divides the number 17 totally (without leaving any remainder) or not, if it divides, then increment the**count**variable and use**break**keyword to exit from the inner**for**loop - Now check whether
**count**holds it original or initial value or not. If it holds, then print the number as prime number in first column, that is without adding horizontal tab before the number - And if
**count**holds any value other than 0, then print the number as composite number in second column, that is after some horizontal tab - Never forgot to initialize 0 again to the
**count**variable before going back to the first**for**loop to again check and do the same steps as told above - In this way all the prime and composite numbers gets printed as output

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:

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:

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:

#### Same Program in Other Languages

« Previous Program Next Program »

Like/Share Us on Facebook 😋