Thread: Do-while statements

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    6

    Do-while statements

    I am very new to C programming and am trying to create a program as part of an assignment.

    The idea is to make a program to evaluate a summation series, E.

    E=1+ 1/1! +1/2! + 1/3! +1/4! ........

    The program should evaluate the series up to the point where one of the terms in it is less than a user specified limit. It then displays the series summation on the screen and tells the user the number of terms that it evaluated to calculate the series.

    I have tried using a do-while statement. At the moment, any number entered by the user that is less than one gives a result of 2.0000 which is not right and a number entered greater than 1 causes the do loop to run for ever. Surely if the number entered is greater than one it should execute the statements in the loop once and then break out of the loop as none of the terms can possibly be greater than 1.

    I am not sure if I have the order of expressions right in the loop or whether I have left out an expression that should be in the loop. Also, I am not sure whether I have entered the while logical expression correctly: - Does C interpret the word "input" that I have put in the logical expression as the input variable defined at the beginning of the code or just as an undefined word.

    The code is below.

    Thank you!

    Paul

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    main()
    {
          int n, no_of_terms;
         float E, term, input;
    
    printf("Input minimum value of a term: ");
    scanf("%f", &input);
            n=1;
            E=1.0;
            
                       do 
                          {
                           
                           term=1.0/n ;
                           E = E+term ;
                           n = n + 1 ;
                           no_of_terms = n + 1;
                          }
           
            while ( term < input);
                 printf("E=%f \n" , E);
                 printf("Number of terms in evaluation: %f \n",no_of_terms);
                system ("pause");
                
                }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You do have one thing you need to fix:
    Code:
    euler.c:26: warning: format ‘%f’ expects type ‘double’, but argument 2 has type ‘int’
    As to the problem, I would guess you have your while condition backwards. For instance if input is 0.25; the first computed term is 1.0; 1.0 < 0.25 is false, so the loop stops.

  3. #3
    Registered User Cooloorful's Avatar
    Join Date
    Feb 2009
    Posts
    59
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    main()
    {
          int n, no_of_terms;
         float E, term, input;
    
    printf("Input minimum value of a term: ");
    scanf("%f", &input);
            n=1;
            E=1.0;
            
                       do 
                          {
                           
                           term=1.0/n ; <-- that doesn't seem very much like factorial to me.
                           E = E+term ;
                           n = n + 1 ;
                           no_of_terms = n + 1;
                          }
           
            while ( term < input);
                 printf("E=%f \n" , E);
                 printf("Number of terms in evaluation: %f \n",(double)no_of_terms);
                system ("pause");
                
                }

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by tabstop View Post
    You do have one thing you need to fix:
    You obviously aren't counting the horrible random indentation then .
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. Explanation of switch statements
    By ammochck21 in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2006, 02:59 PM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  5. Need help with "if" statements
    By Harryt123 in forum C Programming
    Replies: 22
    Last Post: 05-14-2006, 08:18 AM