Skip to main content

Featured

How to make a simple calculator with GUI in JAVA (using Netbeans)?

First of all create a new package , then create a simple class. Then create a new JFrame Form class. On the right side you will find every tool needed like buttons textfield radio buttons etc. We'll need the buttons and text field and a JLabel Drag and drop required buttons you want to appear on the calculator eg. 1,2,3,4,5,6,7,8,9,0,+,-,*,/,=,AC,OFF arrange them all then on double clicking any button you will enter the source code tab, for digits 0-9 you have to write this code:         String Number =answer.getText() +  one.getText();         answer.setText(Number); Here answer is the jTextFields variable name you can right click the text field and click change variable name. similarly you have to set the code for every digit then for operations you have to write the code: for "+" operation: firstnumber = Double.parseDouble(answer.getText());         jLabel1.setText(answer.getText()+"+"); ...

Calculate the value of e^x

Question: 

Compute the value of e^x
by using the formula 1 + x/1! + x/2! + x/3! + ...

Prompt the user for the desired accuracy of e (i.e number of terms in the summation)  

Answer:

We will make the code of the equation in fraction ignoring the 1.
Then in the end we will add the sum of fraction with 1 and displaying the result.

Suppose we want to calculate the value of e^x and its accuracy up-to 6 terms.

Check out the code below and screenshots.



Code:

#include <iostream>
using namespace std;
int main()
{

 float x,terms,sum=0,counter,power,counter2,counter3,fact;

 cout<<"Enter power of e i.e x= ";
 cin>>x;
 cout<<"Enter the number of terms upto which you require the accuracy of e: ";
 cin>>terms;

 for(counter=1;counter<terms;counter++)
 {
  power=1;fact=1;
  for(counter2=1;counter2<=counter;counter2++)
  {
   power=power*x;
  }

  for(counter3=1;counter3<=counter;counter3++)
  {
   fact=fact*counter3;
  }
  
  sum=sum+(power/fact);

 }

 sum=sum+1;
 cout<<endl;
 
 cout<<"Sum of e^"<<x<<"= 1 + ";
 for(int counter4=1;counter4<terms;counter4++)
 {
  
  cout<<x<<"^"<<counter4<<"/"<<counter4<<"!";
  if(counter4<terms-1)
   cout<<" + ";
 }
 
 cout<<" = "<<sum;

 return 0;
}

Screenshots:












If you just want the code to print out only the sum not the whole fraction again then the code and screenshots are below.


Code:




#include <iostream>
using namespace std;
int main()
{

 float x,terms,sum=0,counter,power,counter2,counter3,fact;

 cout<<"Enter power of e i.e x= ";
 cin>>x;
 cout<<"Enter the number of terms upto which you require the accuracy of e: ";
 cin>>terms;

 for(counter=1;counter<terms;counter++)
 {
  power=1;fact=1;
  for(counter2=1;counter2<=counter;counter2++)
  {
   power=power*x;
  }

  for(counter3=1;counter3<=counter;counter3++)
  {
   fact=fact*counter3;
  }
  
  sum=sum+(power/fact);

 }

 sum=sum+1;
 cout<<endl;
 
 
 cout<<sum;

 return 0;
}



Comments