Function with arguments but no return value

Input:

#include <stdio.h>

void checkPrimeAndDisplay(int n);

int main()

{

int n;

printf("Enter a positive integer: ");

scanf("%d",&n);
    
checkPrimeAndDisplay(n); // n is passed to the function

return 0;

}

void checkPrimeAndDisplay(int n) // return type is void meaning doesn't return any value

{

int i, flag = 0;

for(i=2; i <= n/2; ++i)

{

if(n%i == 0)

{

flag = 1;

break;

}

}

if(flag == 1)

printf("%d is not a prime number.",n);

else

printf("%d is a prime number.", n);

}

Output:

Enter a positive integer: 4

4 is not a prime number
-----------------------------------------------------------------------------------
Enter a positive integer: 5

5 is a prime number

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The integer value entered by the user is passed to the checkPrimeAndDisplay() function.

Here, the checkPrimeAndDisplay() function checks whether the argument passed is a prime number or not and displays the appropriate message.

HOME/BACK

Comments

Popular posts from this blog

Keywords in C Programming

Definition of C Language

Data Types in C Programming