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

  • SEARCH
  • Home
  • 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 8668103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:07:58+00:00 2026-06-12T18:07:58+00:00

package homework1C; public class Homework1C { public static void main(String[] args){ double term =2,sum;

  • 0
package homework1C;

public class Homework1C {

public static void main(String[] args){
    double term =2,sum;
    int n;
    final double difference = 0.0000000001;
    double x;
    for(sum=0.0,n=0;term > difference;n++){


        x = find_n_fact(n);
        term=1.0/x;
         sum+=term;
         n++;
    }

        System.out.printf("e : %f\n", sum);
        System.out.printf("term : %d\n", n);

    }

public static int find_n_fact(int n){
int i;
int fact = 2;
for(i = n; i>2;i--){

    fact *= i;
}
    return fact;
}

}

this is what i was being asked to do :
Write another Java application program to find and display an approximation of e (natural logarithm). Use the following approximation formula starting with n as 2, incrementing by 1 until two successive values of e differ by less than 0.0000000001 and display not only the approximation, but how many terms of n were used in the last approximation. The formula is: approximation of e = 1/0! + 1/1! + 1/2! + 1/3! + … , where n! is n factorial

This is my present output for this program

e : 1.043081
term : 20

what am i doing wrong ? the answer was suppose to be

e: 2.71828
term: 15

How to solve this?

  • 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-12T18:07:59+00:00Added an answer on June 12, 2026 at 6:07 pm

    Several mistakes you have done:

    • Your factorial method was wrong. Although it could be done in iterative manner as you tried, I suggest you the recursive version.
    • You are incrementing n in the for-loop of main() twice, that’s nonsense.

    Here is the fully working code for you:

    public class Homework1C {
        public static void main(String[] args) {
            double term = 2, sum = 0;
            final double difference = 0.0000000001;
            int n;
    
            for (n = 0; term > difference; n++) {
                term = 1.0 / find_n_fact(n);
                sum += term;
            }
    
            System.out.printf("e : %f\n", sum);
            System.out.printf("term : %d\n", n);
        }
    
        public static double find_n_fact(int n) {
    
            if (n == 0 || n == 1)
                return 1.0;
    
            return n * find_n_fact(n - 1);
        }
    }
    

    And iterative version of factorial method is here:

    public static double find_n_fact(int n) {
        double i, fact = 1;
    
        if(n < 0) // for negative numbers, factorial is nonsense.
            return -1;
    
        for (i = n; i > 1; i--)
            fact *= i;
    
        return fact;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

package org.study.algos; public class Study { public static void main(String[] args) { A a
package GC; import java.util.Scanner; public class main { public static void main(String args[]) {
package com.test01; public class test01 { public static void main(String[] args) { System.out.println(hi); }
package pack; public class sample{ public static void main(String input[]) { NumberFormat numberFormat =
package matlab; import com.mathworks.toolbox.javabuilder.*; import com.eigenface.Eigenface; public class Test { public static void main(String[]
package homework5; import java.io.*; import java.util.Arrays; public class Main { /** * @param args
package src; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Command { public static
package data_structures; import java.util.StringTokenizer; public class ExpressionEvaluator implements Stack, Queue { private String userInput;
package main; class F { void f() { System.out.print(F.f() ); this.g(); } void g()
package macroreader; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class MacroReader { public static

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.