mr7b ..
To calculate the value of e^x ,, I do this code but I THINK I have mistake . . !

Can any one tell me if it's true or not > ?

package calculate;
import java.util.Scanner;

public class Main {
	public static void main(String args[]){
		double x, n, sum=1 ;
		int i;
		Scanner read = new Scanner(System.in);
		System.out.println("Enter the value of x: ");
		x = read.nextDouble();
		System.out.println("Enter the value of n: ");
		n = read.nextDouble();

		    for(i = 1; i <= n; i++)
		    	sum = math(x, i);
		    	    System.out.println("e^x =  "+sum);
	}
                   public static double math(double x, int i){

                   if (i == 1)
                 	return x;

                     else
                         return x /(i-1);
   }
}

code tags .. I can not use it ,, I don't know why >~

I do not get why you do not use Math.exp(x) instead?

I do not get why you do not use Math.exp(x) instead?

How I can do it ?
what I'm doing is not correct ?

The program shown will not calculate the value of e^x!

Presumably, this is an exercise in programming to calculate the value as a series (hence collecting and printing a sum!) for which you should have the formula;

e^x=1+x+(x^2)/2!+(x^3)/3!+(x^4)/4!...

This is no doubt the formula used by the java math class to produce the function Math.exp(x).

What you need is for the for loop to calculate each term of the expression and add them into the summation.

sum+=math(xi)
math(x,i)
{
return x^i/factorial(i)
}

You will therefore need another method or another way to evaluate the factorial of i.

well, factorial is of course and easy task there:

public int factorial(int f)
{
int result = 1;
for(int i = 2; i <= f; i++)
{
result *= i;
}
return result;
}
for(i = 1; i <= n; i++)
		    	sum+ = math(x,i);
		    	    System.out.println("e^x =  "+sum);
        }
                   public static double math( double x, int i){
               {
                 if (i == 1)
                     return x;
                 else;
                     return x^i/factorial(i); 
                }
}
                   
                   public int factorial(int f) {
                       int result = 1;
                   for(int i = 2; i <= f; i++) {
                       result *= i; }
                   return result;
}
}

it gives me an error there :
sum+ = math(x,i);
return x^i/factorial(i);

1. if it is an error, please put all the error text here
2. i am not sure, but maybe x^i is not allowed and

Math.pow(x,i);

has to be used; might also need to make something like

Math.pow((Double)x, (Double)i);

return x^i/factorial(i);

her it gives me an error also : ? it says operator ^ can not be applied to double , int ??

and when I but this
Math.pow((Double)x, (Double)i);
it gives an error in many places >~

return x^((Double)i)/factorial(i);
what about now?

it also gives me an error ..

it sayes :

operator ^ can not be applied to double , int
inconvertible types
required: java.lang.Double
found: int

so I say: write it

((Double)i)

I write it but also it gives me an error >!

import java.util.Scanner;
 
public class Main 
{
	public static void main(String args[])
	{
		double x, n, sum=1 ;
		int i;
		Scanner read = new Scanner(System.in);
		System.out.println("Enter the value of x: ");
		x = read.nextDouble();
		System.out.println("Enter the value of n: ");
		n = read.nextDouble();
 
		for(i = 1; i <= n; i++)
		{
	    	sum += math(x,i);
	    	    System.out.println("e^x =  "+sum);
		}
	}

	public static double math( double x, int i)
	{	
		if (i == 1)
			return x;
        else;
            return Math.pow(x, i)/factorial(i); 	
	}

	public static int factorial(int f) 
	{
       int result = 1;
       for(int i = 2; i <= f; i++) 
       {
    	   result *= i; 
       }
       
       return result;
	}
}

ok, here is completely compilable code :)
the problem was that there is no ^ for raising power in java (that operator does a different job)
sorry, for not getting into problem at once, hope now that solves the problem for you :)

now it's working .

thanks for ur helping

smilie

Some observations.

You declare the method factorial as a class but call it statically like math. Without declaring a factorial object there will be no such method to call. You need to declare an object and call it through that or more likely make the factorial method static like math.

The method math is declared using a double parameter x and and integer i, but the call is made using x and n, both declared as doubles. This will be part of your errors!

You need the type of n in main and i in the math parameter to match. Make both whichever type is needed for the ^ operator.

Member Avatar for ztini

Don't revive old threads. Please start a new thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.