Prime Number program in C using sqrt (square root ) Function

Spread the love

In our previous blog post, we discussed What is prime number and C program to check given number is prime or not. but we used an In-efficient way,  In this post, I am going to write the same program in an Efficient way. using the square root function Instead of comparing all numbers from 2 to number/2.(we used this method in the previous blog post).

Description :

This program accepts one positive number from the user. and try to check that number is a prime number or not. 

C Program to calculate prime number in efficient way using sqrt function:

  1. #include<stdio.h>
  2. #include<math.h>
  3. void main()
  4. {
  5.    int num,i;
  6.    int FLAG=1;
  7.    printf(“Enter any Positive Number : “);
  8.    scanf(“%d”,&num);
  9.    for(i=2;i<=sqrt(num);i++)
  10.    {
  11.       if(num%== 0)
  12.       {
  13.          FLAG = 0;
  14.          break;
  15.       }
  16.    }
  17.    if(FLAG == 1 && num > 0)
  18.    {
  19.       printf(“%d is Prime Number \n,num);
  20.    }
  21.    else
  22.    {
  23.       printf(“%d is not a Prime Number \n,num);
  24.    }
  25.    return;
  26. }

Output :

Prime Number program in C using sqrt function

Quick Notes:

  • Here i used sqrt() function. declaration of sqrt function found in math.h so don’t forget add that header file to our program. if you forget to add that header file you will get one warning message saying

warning: incompatible implicit declaration of built-in function ‘sqrt’ [enabled by default]  for(i=2;i<=sqrt(num);i++)

  • This program compilation is a little different from other program compilations because we need to pass the library file name as a compilation flag. you need to pass compilation flag -lm  to gcc while compiling this program otherwise you will get Linking Error. see below image for better understanding.
prime-number-program-using-sqrt-method
Possible Linking Errors and Warnings

Explanation :

look at the above picture I compiled the program two times the first time without using the compilation flag (i.e lm) I got one warning and one linking error. 
I got a warning because I removed math.h header file from the program.
The linking error is for we are using sqrt function, but that function is unknown to the compiler (because we are not passing -lm here). where to look for that function if you pass lm to gcc it will look into math library and it contains sqrt prototype. so we will not get that linking error.

The sqrt functions in math.h have implementations in libm.so (or .a for static linking), and libm is not linked in by default. So we need to pass compilation flag -lm to gcc to link those functions also.

Related Program on Prime Numbers:

More C programs:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

7 Responses

  1. Aadil Baeg says:

    in the 19th line,instead of if(FLAG == 1),it should be if(FLAG ==1&&num>1)

  2. David Milenkovic says:

    When I enter 4 it says thet 4 is prime.

  1. […] the i (Outer loop variable) is evenly divisible by any number from 2 to j/2 ( We can also use the Square root method to check prime numbers, But for simplicity, we are sticking to this […]

  2. […] C Program to Check Prime Number using Square Root (Sqrt) Method […]

  3. […] C Program to Check Prime Number using Square Root (Sqrt) Method […]

Leave a Reply