Function with no arguments but with a return value

Input:

#include <stdio.h>

int checkPrimeNumber(int n);

int main()

{

int n, flag;

printf("Enter a positive integer: ");

scanf("%d",&n);

flag = checkPrimeNumber(n);
// n is passed to the checkPrimeNumber() function
// the returned value is assigned to the flag variable

if(flag == 1)

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

else

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

return 0;

}

int checkPrimeNumber(int n) // int is returned from the function

{

int i;

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

{

if(n%i == 0)

return 1;

}

return 0;

}

Output:

Enter a positive integer: 4

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

5 is a prime number

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

The input from the user is passed to the checkPrimeNumber() function.

The checkPrimeNumber() function checks whether the passed argument is prime or not.

If the passed argument is a prime number, the function returns 0. If the passed argument is a non-prime number, the function returns 1. The return value is assigned to the flag variable.

Depending on whether a flag is 0 or 1, an appropriate message is printed from the main() function.

HOME/BACK

Comments

Popular posts from this blog

Keywords in C Programming

Definition of C Language

Data Types in C Programming