**C TUTORIAL**

#

**C PROGRAMS**

Function

➤ Introduction to Function

➤ User-defined Functions

➤ Recursion in C

➤ Storage Classes in C

➤ Scope of Variables

Function Programs in C

➤ Functions Examples in C

➤ Add 2 Numbers Function

➤ Find Sum of N numbers

➤ Largest of Three Numbers

➤ Add Subtract Multiply Divide

➤ Calculator Program Functions

➤ Factorial Program Function

➤ GCD of Two Numbers

➤ Find Power of a Number

➤ Find Reverse a Number

➤ Fibonacci Series Function

➤ Prime Number Function

➤ Palindrome Number in C

➤ Armstrong Number in C

Recursion Programs in C

➤ Recursion Examples in C

➤ Fibonacci Series

➤ Find Factorial

➤ Find GCD or HCF

➤ LCM Using Recursion

➤ Tricky C Programs

Previously we developed a C program to check whether the given number is a prime number or not? Now, we will do the same but using a function. In this post, we will write a C program to find the prime number using a function and find all prime numbers in a given range.

**A natural number that has only two factors ( 1 and itself ) is called a prime number.** For example- 5 is a prime number because it has only two factors 1 and 5. Similarly, 9 is not a prime number because it has more than 2 factors that are 1,3, and 9.

A function is a block of code that performs a specific task. For example, the main is a function and every program execution starts from the main function in C programming. The function is a small program that is used to do a particular task. In C a big program divided into several small subroutines/functions/procedures. Hence C is a function-oriented programming language.

The C program is made of one or more pre-defined/user-defined functions. Every program must have at least one function with the name main. The execution of the program always starts from the main function and ends with the main function. The main function can call other functions to do some special task.

## Function to check prime number in C

```
int checkPrime(int number)
{
int count = 0;
for(int i=2; i<=number/2; i++)
{
if(number%i == 0)
{
count=1;
break;
}
}
if(number == 1) count = 1;
return count;
}
```

We don’t have negative prime numbers. A positive integer is prime if it has only two divisors among the positive integers. Zero is neither positive nor negative, hence it, not a prime number. 1 is not considered prime numbers. 2 is the only even prime number.

*-ve numbers:- not prime number 0:- not prime number 1:- not prime number*

We know that prime number having only two factors, so if the number is divisible by any number between 2 to half of the number then the number is not a prime number.

A number will never divisible by the greater than half of the number. For example, half of the number 10 is 5 so, the number is never divisible by greater than 5 (except itself). Hence we should check only for the numbers 2 to half of the number. It will reduce the number of iteration of the loops, and the performance of the program will increase.

## C program to find prime number using a function

```
#include<stdio.h>
int checkPrime(int number)
{
int count = 0;
for(int i=2; i<=number/2; i++)
{
if(number%i == 0)
{
count=1;
break;
}
}
if(number == 1) count = 1;
return count;
}
int main()
{
int number ;
printf("Enter number: ");
scanf("%d",&number);
if(checkPrime(number) == 0)
printf("%d is a prime number.", number);
else
printf("%d is not a prime number.", number);
return 0;
}
```

Output for different test-cases:-

Enter number: 1

1 is not a prime number.

Enter number: 9

9 is not a prime number.

Enter number: 11

11 is a prime number.

## Display all prime numbers in a given range using a function

```
#include<stdio.h>
int checkPrime(int number)
{
int count = 0;
for(int i=2; i<=number/2; i++)
{
if(number%i == 0)
{
count=1;
break;
}
}
if(number == 1) count = 1;
return count;
}
int main()
{
int m, n;
printf("Enter min and max value of the range: ");
scanf("%d %d",&m, &n);
printf("Prime numbers from %d to %d are: ", m, n);
for(int i=m; i<=n; i++)
{
if(checkPrime(i) == 0)
printf("%d\t",i);
}
return 0;
}
```

Output for different test-cases:-

Enter min and max value of the range:` 1 20`

Prime numbers from 1 to 20 are: 2 3 5 7 11 13 17 19

Enter min and max value of the range: `20 50`

Prime numbers from 20 to 50 are: 23 29 31 37 41 43 47

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

BerylPretty! This was an extremely wonderful post. Thanks for supplying this info.

NelleGood day! Do you use Twitter? I’d like to follow you if that would be ok. I’m undoubtedly enjoying your blog and look forward to new updates.

ElkeI every time spent half an hour reading this website’s articles or reviews every day along with a mug of coffee.

Letha ZeraAwesome 🙂

AmanI have been browsing online for greater than three hours lately, but I by no means discovered any interesting article like yours. It is lovely worth sufficient for me. Personally, if all web owners and bloggers made excellent content as you probably did, the net will be a lot more helpful than ever before. Greetings! Very helpful advice within this article! It is the little changes that produce the largest changes. Thanks for sharing! Saved as a favorite, I really like your site!

VikasDear Aman, Thank You very much.