Advertisement
HugoBallee

GLIN202/TP2/exo11

Feb 5th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.66 KB | None | 0 0
  1. // Hugo Gallée
  2. // TP2/exo11.c
  3. // Calculer une racine carree grace a l'algorithme de Heron d'Alexandrie
  4.  
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <math.h> // Utilie pour la valeur absolue (abs) et la fonction sqrt
  9.  
  10.  
  11. int main()
  12. {
  13.     int a, i, n;
  14.     float x, xNext;
  15.  
  16.     printf("Calculer la racine carre de : ");
  17.     a = lire_entier(a);
  18.     printf("Precision : 1/");
  19.     n = lire_entier(n);
  20.  
  21.     x = a;
  22.     xNext = (x / 2.) + ((float) a / (2. * x));
  23.     while (abs(x - xNext) > 1. / n) {
  24.         x = xNext;
  25.         xNext = (x / 2.) + ((float) a / (2. * x));
  26.     }
  27.  
  28.     printf("\n");
  29.     printf("  xNext: %4.5f\n", xNext);
  30.     printf("sqrt(%d): %4.5f\n", a, sqrt((float) a));
  31.  
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement