Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created January 2, 2021 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/e8745f109ba66ac65b373367e5610ee6 to your computer and use it in GitHub Desktop.
Save parzibyte/e8745f109ba66ac65b373367e5610ee6 to your computer and use it in GitHub Desktop.
// https://parzibyte.me/blog
#include <stdio.h>
double valorAbsoluto(double numero)
{
if (numero < 0)
{
return numero * -1;
}
return numero;
}
double raizCuadrada(int numero)
{
double margen = 0.000001;
double estimacion = 1.0;
// Mientras haya una diferencia notable.
// Es decir, que el cuadrado de nuestra estimación difiera mucho del número
while (valorAbsoluto((estimacion * estimacion) - numero) >= margen)
{
double cociente = numero / estimacion;
double promedio = (cociente + estimacion) / 2.0;
// Elimina la siguiente línea si no quieres ver el proceso
printf("Estimación: %lf. Cociente: %lf. Promedio: %lf\n", estimacion, cociente, promedio);
estimacion = promedio;
}
return estimacion;
}
int main(int argc, char const *argv[])
{
int numero;
printf("Ingresa un número: ");
scanf("%d", &numero);
double raiz = raizCuadrada(numero);
printf("La raíz cuadrada de %d es %lf", numero, raiz);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment