Prime or Not Using for statement in C program

//for statement
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,flag=0;
printf("ENter a positive integer:");
scanf("%d",&n);
for(i=2;i<n/2;i++)
{
if(n%i==0)
{
flag=1;
break;

}

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

}

PRIME OR NOT USING RECURSION
//prime or not using recursion
#include <stdio.h>

int primeno(int, int);

int main()
{
int num, check;
printf("Enter a number: ");
scanf("%d", &num);
check = primeno(num, num / 2);
if (check == 1)
{
printf("%d is a prime number\n", num);
}
else
{
printf("%d is not a prime number\n", num);
}
return 0;
}

int primeno(int num, int i)
{
if (i == 1)
{
return 1;
}
else
{
if (num % i == 0)
{
return 0;
}
else
{
return primeno(num, i – 1);
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Enter Captcha Here : *

Reload Image