C for loop control using difference not giving the result -


i writing code keep calculating sequence 1+(2/2!)+(3/3!)+..... until difference between 2 subsequent members of sequence 10^-4 subtraction giving me 0.00 , not stopping loop

#include<stdio.h> #include<stdlib.h>  int main() {   int c = 0;   double sum = 0;   double fact = 1;   double seq;   double temp = 0;    bool check = false;        while (check != true)     {         c++;         fact = fact * c;         seq = c/fact;          if(temp-seq == 1e-04)             check = true;         // printf("%f\n",temp-seq);          temp = seq;       }     printf("stopped @ %d operations \n",c);    system("pause");   return 0; } 

first of all, read other replies, have info. second, not doing calculations right, se below working example.

#include<stdio.h> #include<stdlib.h> #include<stdbool.h>  int main() {         int     count = 0;         double  sequence = 0;         double  previous = sequence;         double  factorial = 1;          bool    done = false;            while (!done)         {                 ++count;                 factorial = factorial * count;                 sequence += count / factorial;                  if (sequence - previous < 1e-4)                         done = true;                  printf("%f\n",sequence - previous);                  previous = sequence;         }     printf("stopped @ %d operations \n", count);    system("pause");   return 0; } 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -