C Program - Prime number or not using function

#include<stdio.h>
// Prototype of function prime returning integer and having int parameter user_num.
int prime(int user_num);

int main()
{
int num,ans;
printf("Enter the Number = "); //Ask the user to insert the number.
scanf("%d", &num);
ans=prime(num);     // Call the prime function to check that number is //prime.
if (ans == 1) // if prime function returns 1, number is prime
printf("%d is prime.\n", num);
else
printf("%d is not prime.\n", num); // Otherwise not prime.
getch();
}

//Definition of function prime.
int prime(int user_num)
{
int div;

for (div=2; div <= user_num-1; div++)      // for loop to check that number //is prime.
{
if (user_num%div == 0) // returns 0 if statement is true.
return 0;
}
if (user_num == div)
return 1; // return 1 if statement is true.
}

Comments

Popular posts from this blog

How to Block Websites without using any Software.

C Program - Greater, less and Equal

C Program - Even Odd