Computer Science, asked by roshu2780, 1 year ago

a program in c to find whether a number is prime or not without using any loop ?​

Answers

Answered by sswaraj04
3

int check(int n, int i)

{

if (i == 1)

{

return 1;

}

else

{

if (n % i == 0)

return 0;

else

return check(n, i - 1);

}

}

int main()

{

int n, val;

printf("Enter number: ");

scanf("%d", &n);

val = check(n, n/ 2);

if (val == 1)

{

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

}

else

{

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

}

return 0;

}

******it's recursive function*****

Answered by Ranauk456
2

// C program to check if a  

// number is prime

 

int main()  

{  

   int n, i, flag = 1;  

 

   // Ask user for input  

   printf("Enter a number: \n");  

 

   // Store input number in a variable  

   scanf("%d", &n);  

 

   // Iterate from 2 to n/2  

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

 

       // If n is divisible by any number between  

       // 2 and n/2, it is not prime  

       if (n % i == 0) {  

           flag = 0;  

           break;  

       }

}  

 

   if (flag == 1) {  

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

   }  

   else {  

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

   }  

 

   return 0;  

}

Similar questions