-
-
Save parzibyte/e8745f109ba66ac65b373367e5610ee6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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