Question

hi I am new to Java and I am stuck on this program I have to...

hi I am new to Java and I am stuck on this program I have to write an application that estimates the value of the mathematical constant e by using the following formula. Allow the user to enter the number of terms to calculate.

e = 1 + 1/1! * 1/2! + 1/3! + ....

Thank you

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program

import java.util.Scanner;
public class Main {
    public static void main(String args[])
    {
        double e = 1;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number of terms to calculate: ");
        int n = input.nextInt();
        for(int x = 1; x <n; x++)
            e = e + 1/(double)factorial(x);
        System.out.println("Value of e: "+ e);
     
    }

    public static double factorial(int n)
    {
        int fact = 1;
        for (int i=2; i<=n; i++)
            fact *= i;
        return fact;
    }
}

Output

Enter the number of terms to calculate: 10
Value of e: 2.7182815255731922

Add a comment
Know the answer?
Add Answer to:
hi I am new to Java and I am stuck on this program I have to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have this c++ program that i have to create but i am stuck on it....

    I have this c++ program that i have to create but i am stuck on it. Write a height conversion program that shall allow user to convert from feet and inches to meters (option 1) and vice versa (option 2). User shall be able to specify the type of conversion (a menu of two options). Display an error message if an invalid option is specified. Otherwise, the program would read in a length in feet and inches (two separate integer...

  • Hi , i have facing problems in both 5 error while i am soloving it can...

    Hi , i have facing problems in both 5 error while i am soloving it can you help me with good solotion for both pleas pleas ??? a- Write a java class to define the new data type Item. The data type Item is defined by the attributed name of type String) and price of type double). Provide the following methods in the class implementation: • Default constructor (that initializes the attribute name to the value “Unknown”, and the attribute...

  • Java GUI Help! Hi, I have a Java GUI exercise that I need to complete and...

    Java GUI Help! Hi, I have a Java GUI exercise that I need to complete and was wondering if I could have a little bit of help... you can see it below. Create a simple graphical application that will enable a user to perform push, pop and peek operations on a stack and display the resulting stack (using toString) in a text area. Any help would be much appreciated! Thank you.

  • Hi, Hi, I need someone to write a program in Phython 3.6, and please I will...

    Hi, Hi, I need someone to write a program in Phython 3.6, and please I will appreciate if the code is error free and indented correctly. The output would be preferable. thanks Write a GUI program that translates the Latin words to English. The window should have three buttons, one for each Latin word. When the user clicks a button, the program displays the English translation in a label. 3. Miles Per Gallon Calculator Write a GUI program that calculates...

  • Hi, I am stuck on a homework assignment where we use SMTP to transmit an email...

    Hi, I am stuck on a homework assignment where we use SMTP to transmit an email across a client/ server using read and send functions. I have read the unistd.h implementation of the functions read and send. Could someone possibly explain via pseudo or example how i could use this in terms of sockets? I will be using a while loop to read byte by byte the message. Not looking for a fully written program or anything , but if...

  • I have completed Machin's formula, but I am having trouble with the other two. This is...

    I have completed Machin's formula, but I am having trouble with the other two. This is because the code's for Leibniz's formula and Ramanujan's formula need to be done using loops not syssum or things like that. PLEASE HELP! Since the advent of computers, a large number of digits of π has been available. However, before that, many mathematicians found ways to approximate T. For example Machin's formula (1706) is 4 arctan - arctan Leibniz (~1750) found that π can...

  • Java I am stuck on, enter a number grade: 9.8 ERROR: 9.8 and enter a number...

    Java I am stuck on, enter a number grade: 9.8 ERROR: 9.8 and enter a number grade: 10 ERROR: 10 Here is the specs ​​​​​​​ Write a class called Grades. Grades must have a main method (see example of what the main method does below). Grades are values between zero and 10 (both zero and 10 included), and are always rounded to the nearest half point (i.e. ALL GRADES entered in the system end in either .0 or .5). To...

  • Java. Java is a new programming language I am learning, and so far I am a...

    Java. Java is a new programming language I am learning, and so far I am a bit troubled about it. Hopefully, I can explain it right. For my assignment, we have to create a class called Student with three private attributes of Name (String), Grade (int), and CName(String). In the driver class, I am suppose to have a total of 3 objects of type Student. Also, the user have to input the data. My problem is that I can get...

  • I need help on creating a while loop for my Java assignment. I am tasked to...

    I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...

  • Hey guys. I am new to coding. And I need your help in answering this problem...

    Hey guys. I am new to coding. And I need your help in answering this problem for me. You have to answer this problem using only C++ programming language. Thanks. "Write a program that asks the user for an integer value, num. The program calculates the total number of factors of num. For example, if the user enters 12, the program should print out 6 (1,2,3,4,6,12)."

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT