import javax.swing.*;
public class trigFun
{
public static void main(String args[])
{
String s;
s=JOptionPane.showInputDialog(null, "Enter number to take factorial of");
int fact=Integer.parseInt(s);
System.out.println("Factorial of("+s+")="+factorial(fact));
s=JOptionPane.showInputDialog(null, "Enter a radian value");
double radian=Double.parseDouble(s);
System.out.println("sin("+radian+")="+sin(radian));
System.out.println("cos("+radian+")="+cos(radian));
System.out.println("tan("+radian+")="+tan(radian));
}
public static double factorial(int a)
{
double s=1;
for(double i=a; i>0; i--)
{
s=s*i;
}
return s;
}
public static double sin(double radian)//calculated using mclauren series
{
int sgn;
if (radian<-3.141592653589793)
sgn=0;
else
sgn=1;
if (sgn==0)
{
while(radian<-3.141592653589793)
{
radian=radian+6.283185307179586;
}
}
if (sgn==1)
{
while(radian>3.141592653589793)
{
radian=radian-6.283185307179586;
}
}
double result=0;
int j=1;
for (double i=1; i<=21;i=i+2)
{ //21 is a large arbitrary value
if(j%2==0)
{
result=result-1*(pow(radian, i))/((double)(factorial((int)(i))));
}
else
{
result=result+1*(pow(radian, i))/((double)(factorial((int)(i))));
}
j=j+1;
}
return result;
}
public static double cos(double radian)
{
int sgn;
if (radian<0)
sgn=0;
else
sgn=1;
if (sgn==0)
{
while(radian<-3.141592653589793)
{
radian=radian+6.283185307179586;
}
}
if (sgn==1)
{
while(radian>3.141592653589793)
{
radian=radian-6.283185307179586;
}
}
double res=0;
int j=1;
for (double i=0; i<=20;i=i+2)
{
if(j%2==0)
{
res=res-1*(pow(radian, i))/((double)(factorial((int)(i))));
}
else
{
res=res+1*(pow(radian, i))/((double)(factorial((int)(i))));
}
j=j+1;
}
return res;
}
public static double tan(double radian)
{
int sgn;
if (radian<0)
sgn=0;
else
sgn=1;
if (sgn==0)
{
while(radian<-3.141592653589793)
{
radian=radian+6.283185307179586;
}
}
if (sgn==1)
{
while(radian>3.141592653589793)
{
radian=radian-6.283185307179586;
}
}
return sin(radian)/cos(radian);
}
public static double pow(double b, double exp)
{ // an incomplete pow function that only calculates correctly for integer exp's
double res=1;
if(exp==0 && b==0)
return 1./0;
if(exp==0 && b !=0)
return 1;
for(int i=1; i<=exp; i++)
{
res=res*b;
}
return res;
}
}
Wednesday
//
Labels:
JAVA PROJECTS
//
1 comments
//
1 comments to "TRIGONOMETRIC FUNCTIONS CALCULATED USING MCLAUREN SERIES IN JAVA"
Powered by Blogger.
Blog Archive
-
▼
2011
(50)
-
▼
August
(12)
- INTRODUCTION TO JAVA
- ABSTRACTION
- INHERITANCE
- ENCAPSULATION
- SIMPLE JAVA PROGRAM
- BABYLONIAN METHOD FOR DETERMINING A SQUARE ROO...
- TRIGONOMETRIC FUNCTIONS CALCULATED USING MCLAUREN ...
- PROJECT ON RESERVATION
- CALCULATOR USING JAVA
- MANAGEMENT OF SCHOOL RECORDS USING JAVA
- MULTITHREADED MATRIX MULTIPLICATION IN JAVA
- PROJECT ON LIBRARY SYSTEM IN JAVA
-
▼
August
(12)
Gulzar says:
Another great work of Java programming of using trigonometric function in Java. Very impressive work share in this blog. Happy to visit this blog.