IvoSilva

[PROG1] Ficha 6 | Exercício 1

Nov 20th, 2011
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. #include <stdio.h>
  2. float factorial(int n)
  3. {
  4.     if (n == 0) return 1 ;
  5.     else return (n * factorial(n-1)) ;
  6. }
  7. int signal (int n)
  8. {
  9.     int i , result = -1 ;
  10.     for (i = 0 ; i < n ; i++) result *= -1 ;
  11.     return result ;
  12. }
  13. float potencia(float x , int n)
  14. {
  15.     int i ;
  16.     float result = x ;
  17.     for (i = 0 ; i < n - 1 ; i++) result *= x ;
  18.     return result ;
  19. }
  20. float mod (float n)
  21. {
  22.     if (n < 0) return (-n) ;
  23.     else return (n) ;
  24. }
  25. float sen (float x , float t)
  26. {
  27.     float termo = x , total = 0 ;
  28.     int i = 2 ;
  29.     while (mod(termo) > t)
  30.     {
  31.         total += termo ;
  32.         termo = signal (i) * (potencia (x , 2 * i - 1) / factorial (2 * i - 1)) ;
  33.         i++ ;
  34.     }
  35.     return total ;
  36. }
  37. int main ()
  38. {
  39.     float x , t ;
  40.     printf ("Qual é o valor de x? ") ;
  41.     scanf ("%f" , &x) ;
  42.     printf ("Qual o valor da tolerância? ") ;
  43.     scanf ("%f" , &t) ;
  44.     printf ("O seno de %.2f é %f\n" , x , sen(x , t)) ;
  45.     return 0 ;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment