Saturday, August 21, 2010

Article | Java - Find The Value Of Taylor Series(Sinx,Cosx,E^x)

Java Program TO FInd The Value Of Sinx, Cosx and E^x by using taylor series
Without Using Any String Functions

Sinx=x-(x^3/3!)+(x^5/5!)-.....

Cos x=1-(x^2/2!)+(x^4/4!)-.....

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




  1. import java.io.*;

  2. class TaylorS

  3. {

  4. public static void main(String arg[]) throws IOException

  5. {

  6. BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

  7. int ch=0,fact,n;

  8. double x,term,sum,i,var;

  9. while(ch!=4)

  10. {

  11. System.out.print("\n\n\n\t----------------");

  12. System.out.print("\n\t******MENU******");

  13. System.out.print("\n\t----------------");

  14. System.out.print("\n\n\t[1].sinx\n\n\t[2].cosx\n\n\t[3].e^x\n\n\t[4].Exit.");

  15. System.out.print("\n\n\tEnter your choice : ");

  16. ch=Integer.parseInt(br.readLine());

  17. switch(ch)

  18. {

  19. case 1 : System.out.print("\n\n\t======Sine Series======");

  20. System.out.print("\n\t-----------------------");

  21. System.out.print("\n\n\tEnter the value of x : ");

  22. x=Double.parseDouble(br.readLine());

  23. System.out.print("\n\n\tEnter the number of terms : ");

  24. n=Integer.parseInt(br.readLine());

  25. var=x;

  26. x=((x*3.14)/180);

  27. sum=x;term=x;

  28. for(i=1;i<n;i++)

  29. {

  30. term*=((-1)*x*x)/(2*i*(2*i-1));

  31. sum+=term;

  32. }

  33. System.out.print("\n\n\tsin"+var+" = "+sum);

  34. break;

  35. case 2 : System.out.print("\n\n\t======Cosine Series======");

  36. System.out.print("\n\t-------------------------");

  37. System.out.print("\n\n\tEnter the value of x : ");

  38. x=Double.parseDouble(br.readLine());

  39. System.out.print("\n\n\tEnter the number of terms : ");

  40. n=Integer.parseInt(br.readLine());

  41. var=x;

  42. x=((x*3.14)/180);

  43. sum=term=1;

  44. for(i=1;i<n;i++)

  45. {

  46. term*=((-1)*x*x)/(2*i*(2*i-1));

  47. sum+=term;

  48. }

  49. System.out.print("\n\n\tsin"+var+" = "+sum);

  50. break;

  51. case 3 :

  52. System.out.print("\n\n\t======Exponential Series======");

  53. System.out.print("\n\t------------------------------");

  54. System.out.print("\n\n\tEnter the value of x : ");

  55. x=Double.parseDouble(br.readLine());

  56. System.out.print("\n\n\tEnter the number of terms : ");

  57. n=Integer.parseInt(br.readLine());

  58. sum=fact=1;var=x;

  59. for(i=1;i<n;i++)

  60. {

  61. fact*=i;

  62. term=Math.pow(x,i)/fact;

  63. sum+=term;

  64. }

  65. System.out.print("\n\n\tsin"+var+" = "+sum);

  66. break;

  67. case 4 : System.out.print("\n\n\tGOOD BYE...");

  68. break;

  69. default : System.out.print("\n\n\tINVALID CHOICE !!\n\n\tTRY AGAIN.");

  70. break;

  71. }

  72. }

  73. }

  74. }

Loading...

Comments :

0 comments to “ Java - Find The Value Of Taylor Series(Sinx,Cosx,E^x) ”

Top Views

BloggerTipsTemplates

CHICKLETS

Enter your email address:

Delivered by FeedBurner

Followers