Hey Guys present u some TrigFun Programs!!!

import javax.swing.*;
public class trigFun
{
 public static void main(String args[])
 {   
    String temp;
    temp=JOptionPane.showInputDialog(null, “Enter an integer to take factorial of”);
    int myFactorial=Integer.parseInt(temp);
    System.out.println(“Factorial of(“+temp+”)=”+factorial(myFactorial));
    temp=JOptionPane.showInputDialog(null, “Enter a radian value”);
    double myRadian=Double.parseDouble(temp);
    System.out.println(“sin(“+myRadian+”)=”+sin(myRadian));
    System.out.println(“cos(“+myRadian+”)=”+cos(myRadian));
    System.out.println(“tan(“+myRadian+”)=”+tan(myRadian));   
 }
 public static double factorial(int n)
 {
    double sum=1;
    for(double i=n; i>0; i–)
    {
        sum=sum*i;
    }
    return sum;
 }
 public static double sin(double radian)//calculated using mclauren series
 {
    int sign;
    if (radian<-3.141592653589793)
     sign=0;
  else
    sign=1;
    if (sign==0)
    {
     while(radian<-3.141592653589793)
     {
      radian=radian+6.283185307179586;
     }
    }
    if (sign==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 sign;
    if (radian<0)
     sign=0;
  else
    sign=1;
    if (sign==0)
    {
     while(radian<-3.141592653589793)
     {
      radian=radian+6.283185307179586;
     }
    }
    if (sign==1)
    {
     while(radian>3.141592653589793)
     {
      radian=radian-6.283185307179586;
     }
    }
  
    double result=0;           
    int j=1;                  
    for (double i=0; i<=20;i=i+2)
    {
     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 tan(double radian)
 {
    int sign;
    if (radian<0)
     sign=0;
  else
    sign=1;
    if (sign==0)
    {
     while(radian<-3.141592653589793)
     {
      radian=radian+6.283185307179586;
     }
    }
    if (sign==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 result=1;
    if(exp==0 && b==0)
     return 1./0;
    if(exp==0 && b !=0)
     return 1;    
    for(int i=1; i<=exp; i++)
    {
      result=result*b;
    }
    return result;
 }
}

Advertisements

3 comments on “Hey Guys present u some TrigFun Programs!!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s