I have to write a program that finds square root of an integer that is input by a user. But I met a problem – program does not calculate the sqrt. I searched for the problem on the Internet, read all solutions, but they did not help me. On this forum there are many questions on this topic, but solutions described there are not working for me.
Actually, sqrt function does not work when an integer is input by a user. When I set a value it works perfectly. In other words, it finds a sqrt of constant, but not of variable.
I compile it with this code(as everywhere is written) gcc -o sqrt -lm sqrt.c
I am working on Ubuntu.
This is the code:
#include<stdio.h>
#include<math.h>
main()
{
int a;
double b=sqrt(a);
scanf("%i", &a);
printf("sqrt of integer is %lf", b);
getchar();
getchar();
return 0;
}
You’re using the variable (
a) before initializing it. Thescanfshould go before thesqrt.