A program to check Prime number using Functions.

Prime number using Functions


            In this example, we will understand to check Prime number using Functions. and know how to write, compile and debug it in C language and also learn how to implement it in c.

        
        In order to do this you will need
  • Basic knowledge of c programming language.
  • As well as know how to operate codeblocks.   

Function  Mechanism

            C program does not execute the statements in a function until the function is called.When it is called, the program can send information to the function in the form of one or more arguments although it is not a mandatory.Argument is a program data needed by the function to perform its task.When the function finished processing, program returns to the same location which called the function.


So let's begin with our program

1.Open a codeblocks software.

2.Open new empty file.

3.Copy and paste the code from below. 


A program to check Prime number using Functions,prime numbers program in c,2021,input output code


Declaring a function or prototype:

The general structure of a function declaration is as follows:
  
return_type function_name(arguments); 
Before defining a function, it is required to declare the function i.e. to specify the function
prototype. A function declaration is followed by a semicolon " ; ". Unlike the function
definition only data type are to be mentioned for arguments in the function declaration.
 
Calling a function:

The function call is made as follows:
function_name(arguments);
 
Defining a function:

All the statements or the operations to be performed by a function are given in the function
definition which is normally given at the end of the program outside the main.
Function is defined as follows
return_type function_name(arguments)
{
Statements;
           }


Input code-

 //Program to check Prime number using Functions.  
 #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("\n%d is a prime number.", number);  
  else  
  printf("\n%d is not a prime number.", number);  
  return 0;  
 }  

Now simply build the code. Look for any error. Now run the code.

For output enter any number you want to check whether it is prime number or not.



A program to check Prime number using Functions,prime numbers program in c,2021,input output code


Click the following button to download a program to check Prime number using Functions.





Thats it.Thank you for scrolling.

Post a Comment

If you have any doubt or suggestions.please let me know.

Previous Post Next Post

Ads by google

Ads by google