24/03/2023

Program to Check Prime Number in C Using Function

Write a Program to Check Prime Number in C between 1 to 100. A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime because the only ways of writing it as a product, 1 × 5 or 5 × 1, involve 5. Enter the range to find all the Prime numbers within the range.

Program to Check Prime Number in C

Program to Check Prime Number in C

#include <stdio.h>

int check_prime(int num);

int main()
{
	int n1, n2, i, flag;
	printf("Enter two numbers(intervals): ");
	scanf("%d %d", &n1, &n2);
	printf("Prime numbers between %d and %d are: ", n1, n2);
	for (i = n1 + 1; i < n2; ++i)
	{
		flag = check_prime(i);
		if (flag == 0)
			printf("%d ", i);
	}

	return 0;
}

int check_prime(int num) /*User-defined function to check prime number*/
{
	int j, flag = 0;
	for (j = 2; j <= num / 2; ++j)
	{
		if (num % j == 0)
		{
			flag = 1;
			break;
		}
	}

	return flag;
}

Condition for Prime Number in C


A number that can only be divisible by Itself or 1. You can see that it's clear a number is divisible by itself or one then use a loop starting from one like condition=1 and max condition up to <=Number and divide a number by condition and increase the counter by ++ if the number divided after the end of the count is equal to 2 then the number is prime otherwise not.

The Output of Prime Numbers in C


The Output of Prime Numbers in C

Similar to Prime Number


Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: