Results 1 to 15 of 15
  1. #1
    javawreker is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default some code explanation please?

    editing the initial code to correct the syntax


    Java Code:
    import java.io.*;
    class factorial
    {
    public static void main (String args []) throws IOException
    {
    
    int n2,fac=1;
    String n1;
    
    System.out.println("\n enter the number");
    
    BufferedReader in= new BufferedReader(new InputStreamReader (System.in));
    
    n1= in.readLine();
    n2=Integer.parseInt(n1);
    
    
    
    for( int i=1; i<= n2; i++)
    
    {
    
    
    fac=fac*i;
    }
    
    System.out.println("\n factorial" + fac);
    
    }

    i am new to java.. this is my first lab question.. i am in college

    how the hell am i supposed to remember all these things?
    Last edited by javawreker; 12-31-2011 at 06:05 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: some code explanation please?

    Perhaps you should change courses if you find that there are too many details to remember.
    No one remembers all the details. You need to know where to look to find the answers.

    If you have any questions about the code you posted, please ask.

  3. #3
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default Re: some code explanation please?

    What are you having difficulty with? For the basics of the language, including syntax, try the tutorial. For the methods of objects like BufferedReader, look it up in the API.

    A few observations:

    Java Code:
    // This won't compile. BufferedReader needs to be one word
    Buffered Reader in= new Buffered Reader(new InputStreamReader (System.in));
    
    // Neither will this. Capitalisation matters; it needs to be readLine()
    n1= in.readline();
    
    // This does nothing. What happens to a number when you multiply it by 1?
    fac=fac*1;

  4. #4
    javawreker is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: some code explanation please?

    .i copied the code from someone else note book..i have a java exam coming up in a month...i failed the first time .. did not attend the second time and this is my last time ...

    i am supposed to write a program that takes a number as input and displays it factorial...

    teachers teaching these subjects at college suckes bawls... they have never explained anything.. even they dont have much clue...
    Last edited by javawreker; 12-31-2011 at 01:45 PM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: some code explanation please?

    Please explain your problem and/or ask some questions about what problems you are having.

    Do you understand how to compute a factorial if you do it by hand with paper and pencil?
    What are the steps if you write them down, one simple step at a time to compute 5 factorial?
    Look at those steps and then try to generalize what is done in each step

  6. #6
    javawreker is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: some code explanation please?

    its ok ..am leaving this shit

    all i know is factorial of 5 is 5*4*3*2*1 = 120

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: some code explanation please?

    Now write down how you can do the computation by only having one multiplication (*) on each line using a variable to save the value from the last multiply.

  8. #8
    javawreker is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: some code explanation please?

    i dont know how to ?can you please show me how to ?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: some code explanation please?

    I'll give you the first line:
    fac = 1;

    Now what should be the next thing you do to compute two factorial? What do you multiply fac by?

  10. #10
    javawreker is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: some code explanation please?

    n+1 ?
    fac =fac*n

    while n<=5 ; n++
    Last edited by javawreker; 12-31-2011 at 03:00 PM.

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: some code explanation please?

    Quote Originally Posted by javawreker View Post
    its ok ..am leaving this shit
    If everything that people don't understand would be shit, the world would be full of it; erm, I guess the world is full of stupid people. You have to improve your knowledge and understanding or you are going to fail a third time. All you have to know is how to multiply 1*2*3*4*5 ... Java normally uses a loop for that where the loop counter represents a single term used for the multiplication.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: some code explanation please?

    Close. Now write it in a program, compile and execute it.

  13. #13
    javawreker is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: some code explanation please?

    Quote Originally Posted by JosAH View Post
    If everything that people don't understand would be shit, the world would be full of it; erm, I guess the world is full of stupid people. You have to improve your knowledge and understanding or you are going to fail a third time. All you have to know is how to multiply 1*2*3*4*5 ... Java normally uses a loop for that where the loop counter represents a single term used for the multiplication.

    kind regards,

    Jos
    this is actually my 5th language which i am trying to learn...

    c c++ java php asp..

    everything is in my syllabus...

    i was doing a php project with mysql database until yesterday...got so fed up and confused..that i decided to do java.. opened up my note and this was the first problem in it..

    many of the things in java are really new to me..

    i didnt mean i am leaving this forum shit... i meant i was planning to leave my college without a degree.. because this was not going anywhere


    yes am really a noob..

    let me see if i can get a working sample code in java to find a factorial...

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: some code explanation please?

    Quote Originally Posted by javawreker View Post
    this is actually my 5th language which i am trying to learn...

    c c++ java php asp..

    everything is in my syllabus...

    i was doing a php project with mysql database until yesterday...got so fed up and confused..that i decided to do java.. opened up my note and this was the first problem in it..

    many of the things in java are really new to me..

    i didnt mean i am leaving this forum shit... i meant i was planning to leave my college without a degree.. because this was not going anywhere


    yes am really a noob..

    let me see if i can get a working sample code in java to find a factorial...
    You almost have your code; it's just the 'fac= fac*1' that doesn't make sense (it doesn't do anything); make it 'fac= fac*i' because 'i' represents the current term in the factorial product.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    javawreker is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: some code explanation please?

    ok thanks for all the help

Similar Threads

  1. Looking for explanation of equation
    By flyersjoe in forum New To Java
    Replies: 9
    Last Post: 09-13-2011, 05:08 AM
  2. Need a java code and explanation for this
    By Crossover in forum New To Java
    Replies: 4
    Last Post: 09-21-2010, 01:23 AM
  3. need explanation
    By marie in forum New To Java
    Replies: 2
    Last Post: 03-21-2010, 03:35 PM
  4. need code explanation
    By marie in forum New To Java
    Replies: 3
    Last Post: 03-05-2010, 12:41 AM
  5. need a little explanation
    By cew27 in forum New To Java
    Replies: 7
    Last Post: 12-13-2007, 11:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •