Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Sine function, can't get factorial to work

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Sine function, can't get factorial to work

    I've been having issues with trying to get this program to work. The program's purpose is to compute the sine function without using the Math.sin(). At first I had trouble with the for loop but I finally understood how it worked, so it's not much of an issue anymore. My next problem is trying to get this factorial to work. I've look at tutorials and I think I'm doing the right thing but when I compile I get several errors (stated in code).

    Original Code

    /* This program is suppose to compute the Sine function without using Math.sin */
     
    import java.io.*;
    public class x
    {
    public static void main(String args[]) throws IOException
    {
     
    BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));
     
    double Rad, Sine;
    int MaxE;
    Sine = 0.0;
     
    System.out.println("Enter Radians: ");
    Rad = Double.parseDouble(keybd.readLine());
     
    for (int a = 1; a <= 11; a++)
    {
    Sine += Math.pow(-1,a)*Math.pow(Rad,2*a+1)/factorial(2*a+1);
    }
     
    System.out.println("The value of Sine is " + Sine);
     
    }
     
    }

    Errors

    x.java:20: cannot find symbol
    symbol  : method factorial(int)
    location: class x
    Sine += Math.pow(-1,a)*Math.pow(Rad,2*a+1)/factorial(2*a+1);
                                               ^
    1 error

    I think I'm on the right track but I'm not sure how to troubleshoot these errors yet since I'm still pretty new. Any help is appreciated!
    Last edited by Actinistia; March 16th, 2011 at 02:56 PM. Reason: I shortened up the code to what I needed help with specifically


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Sine function, can't get factorial to work

    Hello Actinistia.

    The issue is that factorial(2 * a + 1) is seen as a method and obviousally there isn't this method within your code.
    factorial isn't even a variable within your code so what are you attempting to do here please?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: Sine function, can't get factorial to work

    Quote Originally Posted by JavaPF View Post
    Hello Actinistia.

    The issue is that factorial(2 * a + 1) is seen as a method and obviousally there isn't this method within your code.
    factorial isn't even a variable within your code so what are you attempting to do here please?
    Well, I was looking at some code from a different forum, although I wasn't able to get it to work either, and it seemed to make some sense. I tried to incorporate it within my own code but wasn't able to do it successfully. Here's the code I'm referring to:

    public double sin(int n){
       double sum = 0.0;
       for(int i = 1; i <= n; i++){
    	//if i is even, -1 becomes 1
       //else, -1 stays negative
    	   sum += Math.pow(-1,i)/factorial(2*i+1); 
    	  }
       return sum;
    }
     
    public double factorial(int i){
    	 if(i <= 1) return 1;
    	 double product = 1;
    	 for(int j = i; j > 1; j--) product *= j; 
    	 return product;
    }

    It's probably due to my own inexperience since I've only been working with java for a few weeks or so. I did have the
    public double factorial(int i){
    	 if(i <= 1) return 1;
    	 double product = 1;
    	 for(int j = i; j > 1; j--) product *= j; 
    	 return product;
    }

    in there before but I was getting errors so I just decided to delete it since I didn't know what to do with it.

  4. #4
    Banned
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Sine function, can't get factorial to work

    just this
    import javax.swing.*;

    class fak {

    public static void main(String[]args) {

    int hasil=1;

    int f=Integer.parseInt(JOptionPane.showInputDialog("Ma sukkan Angka : "));

    for(int i=1;i<=f;i++)

    {

    hasil=hasil*i;

    if(f !=i)

    System.out.print(i+"x");

    else

    System.out.print(i+" = ");

    }

    JOptionPane.showMessageDialog(null,"Hasil dari "+f+"! adalah "+hasil);

    System.exit(0);

    }

    }

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Sine function, can't get factorial to work

    Quote Originally Posted by erdy_rezki View Post
    just this
    What is that? And why resurrect a year old post, especially when the contribution is gibberish.

Similar Threads

  1. [SOLVED] Is it possible to get factorial of negative number
    By Lokesh in forum Java Theory & Questions
    Replies: 3
    Last Post: August 4th, 2011, 05:45 PM
  2. [SOLVED] Using for loop to calculate sine function (factorial issue)
    By Actinistia in forum Loops & Control Statements
    Replies: 2
    Last Post: March 11th, 2011, 03:38 PM
  3. Factorial and Factorion, that is the question!
    By Java Neil in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 25th, 2011, 07:54 PM
  4. [SOLVED] Factorial of 1-20
    By pitchblack in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 16th, 2011, 01:33 PM
  5. Opposites of Sine Cosine and Tangent
    By sp11k3t3ht3rd in forum Java Theory & Questions
    Replies: 2
    Last Post: January 28th, 2011, 01:59 PM