Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8997893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:57:02+00:00 2026-06-15T23:57:02+00:00

I wrote this piece of code. I think that is all right but when

  • 0

I wrote this piece of code. I think that is all right but when I run it, I obtain a bad result. This code is for calculate the Euler number. I will appreciate your answer.

The result that I expect is approximately 2.718281828459045 and I obtain the result 2.718281745910644:

  • 2.718281828459045 (expected)
  • 2.718281745910644 (actual)

Code:

#include <stdio.h>

main() {

    int factor, counter, n = 1;
    float total = 0, division;

    while ( n <= 20 ) {
        counter = 1;
        factor = n;

    while ( counter < n ) {
        factor *= ( n - counter );
        counter++;
    } 

    division = 1.0 / factor;
    total = total + division;
    n++;
    }

    total = total + 1;

    printf( "La constante matematica e vale aproximadamente: %.20f\n", total);

    return 0;  

} /* Finaliza funcion main */
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:57:03+00:00Added an answer on June 15, 2026 at 11:57 pm
    while ( n <= 20 ) {
        contador = 1;
        factorial = n;
    
        while ( contador < n ) {
            factorial *= ( n - contador );
            contador++;
        } 
        // snip
        n++;
    

    int, if it is a 32-bit integer type, can only hold the factorials up to 12!. 13! = 6227020800 is too large for a 32-bit integer. Thus you have overflow, and the results are completely wrong.

    You could get somewhat good results if factorial were a double or a 64-bit integer instead of an int.

    The (relatively small) error your computation gives you is due to using float instead of double for total and division:

    We compute a good approximation to e as a double

    Prelude Text.FShow.RealFloat> FD $ exp 1
    2.718281828459045090795598298427648842334747314453125
    

    and convert that to a float:

    Prelude Text.FShow.RealFloat> FF $ realToFrac it
    2.71828174591064453125
    

    Which is the value you got: 2.718281745910644 modulo the different precision in printing. And it’s the same we get when computing exp 1 as a float:

    Prelude Text.FShow.RealFloat> FF $ exp 1
    2.71828174591064453125
    

    the closest float value to the desired result:

    Prelude Text.FShow.RealFloat> FF 2.718281828459045
    2.71828174591064453125
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this piece of code that lists all JPG files in the current
I wrote this piece of code that is supposed to redirect something written on
I have a program I wrote in Windows with this piece of code that
I'm sorry, but a while ago I wrote a piece of code that was
I've wrote this simple piece of code. And I have a slight problem with
I wrote this code for zoom in/out . it works but even with one
Here is nice piece of code that works fair in all browsers: http://www.imaputz.com/cssStuff/bigFourVersion.html Since
Hey, for this piece of code, the person who wrote the system communicates data
Is it worth to write this piece of code: RelayCommand _saveCommand; public ICommand SaveCommand
This is a piece of code I write for this question: Entry text on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.