Thread: Calculate E

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    2

    Unhappy Calculate E

    I am trying to make a program to calculate E.
    I finished, but it kept returning
    2.000000
    I've looked over this code many times and I can't find an apparent problem.
    Here is the code:
    Code:
    // e_calculate.cpp : Defines the entry point for the console application.//
    
    
    #include "stdafx.h"
    
    
    
    
    int main()
    {
        printf("Enter an integer here\n/");
        int num;
        double e;
        int factorial, count, count2;
        count = 0;
        count2 = 0;
        factorial = 0;
        scanf("%d", &num);
        e = 1;
        while (num > count)
        {
            count = count + 1;
            factorial = count;
            while (count > count2)
            {
                count2 = count2 + 1;
                factorial = factorial * count2;
            }
            count2 = 0;
            e = e + (1 / factorial);
        }
        printf("%f", e);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    e = e + (1 / factorial);
    "factorial" is an int, so "1 / factorial" produces a truncated integer quotient.

    Try "1.0 / factorial" instead.

  3. #3
    Registered User
    Join Date
    Apr 2015
    Location
    Bangalore, Karnataka, India
    Posts
    34
    Yes, as matticus said the operation involves double and integer values. I think you need to convert the integer values to double by typecasting and see what happens.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by www.infysim.org View Post
    I think you need to convert the integer values to double by typecasting and see what happens.
    Only one of the operands to the division has to be double, which will cause the other operand to be automatically promoted to double*. Since one of the operands is a constant, the OP can avoid type-casting by explicitly making that constant a double, as I illustrated. If both operands were variables, then a type-cast would be necessary.

    * See "6.3.1.8 Usual arithmetic conversions" in the specification.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by www.infysim.org View Post
    Yes, as matticus said the operation involves double and integer values. I think you need to convert the integer values to double by typecasting and see what happens.
    Casting is the wrong answer. Use the language to your advantage. Do as Matticus says, and divide the double literal 1.0 by factorial, rather than the integer literal 1.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calculate 100! bug.
    By nimitzhunter in forum C++ Programming
    Replies: 11
    Last Post: 03-29-2014, 11:50 PM
  2. Calculate BMI and BMR
    By nguystep in forum C Programming
    Replies: 4
    Last Post: 10-26-2011, 02:59 AM
  3. Calculate time
    By Goo in forum C++ Programming
    Replies: 2
    Last Post: 05-16-2009, 05:24 PM
  4. calculate age from date
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 08-22-2005, 08:57 AM
  5. calculate the ln of a number?
    By willc0de4food in forum C Programming
    Replies: 9
    Last Post: 04-04-2005, 04:39 PM