Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding Square Root in C programming [duplicate]

Tags:

c

square-root

I am trying to find the square root in C programming. But I am getting error as undefined reference to the sqrt. My Code is:

#include<stdio.h>
#include<math.h>
void main(void){
int x;
int y;
printf("Enter two number numbers");
scanf("%d", &x);
scanf("%d", &y);
int result;
result = ( x * x ) + ( y * y );
double finalresult = sqrt(result);
printf("%f\n", finalresult);
}
like image 624
Jaspreet Deol Avatar asked Jul 11 '26 06:07

Jaspreet Deol


1 Answers

If you're compiling with gcc, math functions are provided by libm.a which you need to link separately using -lm

gcc -Wall main.c -o my_prog -lm
like image 113
simonc Avatar answered Jul 13 '26 12:07

simonc