0% found this document useful (0 votes)
2K views

Prime Number Using C Program

This C program document contains code to check if a given number is prime or not using two different approaches - a function within main and a separate user-defined function. The code takes a number as input, then uses a for loop to check if the number is perfectly divisible by any number between 2 and half of the input number, returning prime or not prime accordingly.

Uploaded by

Kni8fyre 7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

Prime Number Using C Program

This C program document contains code to check if a given number is prime or not using two different approaches - a function within main and a separate user-defined function. The code takes a number as input, then uses a for loop to check if the number is perfectly divisible by any number between 2 and half of the input number, returning prime or not prime accordingly.

Uploaded by

Kni8fyre 7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Check Prime Number using C program

/*Program to check entered number is whether prime or not.*/

#include <stdio.h>

int main()
{
int tally;
int number;
unsigned char flag=0;

printf("Enter an integer number : ");


scanf("%d",&number);

for(tally=2; tally<=(number/2); tally++)


{
if(number%tally ==0)
{
flag=1;
break;
}
}

if(flag==0)
printf("\n%d is a prime number.",number);
else
printf("\n%d is not a prime number.",number);

return 0;
}
Using User Define Function

/*Program to check entered number is whether prime or not.*/

#include <stdio.h>

/*function to check number is Prime or Not*/


int isPrime(int num)
{
unsigned char flag=0;
int tally;

for(tally=2; tally<=(num/2); tally++)


{
if(num%tally ==0)
{
flag=1;
break;
}
}

if(flag==0)
return 1; /*prime number*/
else
return 0; /*not a prime number*/
}

int main()
{
int number;

printf("Enter an integer number : ");


scanf("%d",&number);
if(isPrime(number))
printf("\n%d is a prime number.",number);
else
printf("\n%d is not a prime number.",number);

return 0;
}

You might also like