t1nman

lab1_3

Apr 30th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.71 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. /* Exponent calculating with eps accuracy, 0<eps<1 */
  4.  
  5. int fact(int num);
  6. int cntNum(double num);
  7.  
  8. main()
  9. {
  10.     double eps, summand = 1.0, exp = 0.0;
  11.     int n = 0, size;
  12.  
  13.     puts("enter epsilon's value");
  14.     scanf("%g", &eps);
  15.    
  16.     while (summand > eps) {
  17.         summand = 1.0 / fact(n);
  18.         exp += summand;
  19.         ++n;
  20.     }
  21.  
  22.     size = cntNum(eps);
  23.     printf("%.*g\n", size, exp);
  24.    
  25.     return 0;
  26. }
  27.  
  28. int fact(int num)
  29. {
  30.     int i;
  31.     long factNum = 1;
  32.  
  33.     for (i = 1; i <= num; ++i)
  34.         factNum *= i;
  35.    
  36.     return factNum;
  37. }
  38.  
  39. int cntNum(double num)
  40. {
  41.     int i = 0;
  42.  
  43.     while (num != 1) {
  44.         num *= 10;
  45.         ++i;
  46.     }
  47.     return i;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment