C Program to Check Prime Number using For loop, Recursion and Function

C program for prime number : The following program checks and finds the prime numbers from 1 to 100. It also checks prime numbers using function and recurison in C.

Algorithm to check prime number:

Any number is divisible by other numbers which are less than or equal to half that number. For example, number 14 is divisible by numbers which are less equal 7 i.e 1 and 7. In general a number n is divisible by numbers less than or equal to n/2. So we can use this concept to check whether a given number is prime or not.

C program to check prime number

/* C program to check whether a number is prime using for loop and if-else*/

#include<stdio.h>
#include<stdlib.h>

int main()
{ 
	int n,i,flag=0;
 
	printf("\n Enter any number to check whether it is prime or not:- ");
	scanf("%d",&n);
  
	if (n<2)
	{
	printf("\n %d is not a prime number\n \n",n);
	exit(1); 
	} 
 
	for (i=2;i<=n/2;i++)
	{
		if (n%i==0)
		{ 
		flag=1;
		break;
		}
	} 

	if (flag==0)
	{
	printf("\n %d is prime number \n \n",n);
	} 
	else 
	{ 
	printf("\n %d is not prime number \n \n",n);
	}

	return 0;
}

/* Output of above code / Runtime Test Cases:

 Enter any number to check whether it is prime or not:- 1

 1 is not a prime number

 Enter any number to check whether it is prime or not:- 29

 29 is prime number 

*/
factorial program in C

Now we will write a C program to find prime numbers between two intervals, that is between a given range. For example prime numbers between 1 to n, prime numbers between 1 to 100 or 1 to 500 or 1000.

C program to find prime numbers between 1 to 100 (a range)

/* C program to find prime numbers between a range */

#include<stdio.h>

int main()
{ 
	int lower_limit,n,upper_limit,i,prime;
 
	printf("\n \n Enter the range x and y : ");
	scanf("%d%d",&lower_limit,&upper_limit);
  
	for(n=(lower_limit+1);n<upper_limit;n++)
	{
		prime=1;
  
		for(i=2;i<=n/2;++i)
		{ 
			if (n%i==0)
			{ 
			prime=0;
			break;
			}   
		}

		if (prime==1)
			printf("%d ",n);
	}

	return 0;
}

/* Output of above code:-
    
 Enter the range x and y : 1 100
        
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 */

C program to check prime number using function

/* C program to check prime number using function */

#include<stdio.h>
#include<stdlib.h>

void isPrime(int n); // isPrime Function Prototype

int main()
{
	int n;

	printf("\n Enter any value:- ");
	scanf("%d",&n);

	isPrime(n);
}

// isPrime Function
void isPrime(int n)
{
	int i,LED=0; 

	if(n<2)
	printf("\n %d is not prime number \n \n",n);
	exit(1);
   
	for(i=2;i<=n/2;++i)
	{
	if(n%i==0)
	LED=1;
	break;
	}

	if(LED==0)
		:printf("\n %d is prime number \n \n",n);
	else
		:printf("\n %d is not prime number \n \n",n);

&$x9;return 0;
}

/* output of above code:-

 Enter any value:- 2

 2 is prime number 

*/

C program to check prime number using recursion

/* C Program to find whether a Number is Prime or Not using Recursion */

#include<stdio.h>

int prime_number(int, int);

int main()
{

	int num, check;

	printf("Enter a number: ");
	scanf("%d", &num);

	check = prime_number(num, num / 2);

	if (check == 1)
	{
		printf("%d is a prime number\n", num);
	}
	else
	{
	printf("%d is not a prime number\n", num);
	}

	return 0;

}

int prime_number(int num, int i)
{
	if (i == 1)
	{
	return 1;
	}
	else
	{
		if (num % i == 0)
		{
			return 0;
		}
		else
		{
			return primeno(num, i - 1);
		}
	}
}

/* Output of above code / Runtime Cases :-

Enter a number: 789
789 is not a prime number
 
Enter a number: 751
751 is a prime number

*/

Related C Programs