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 8 of 8

Thread: Factorial of a Number (program completed but doubt in that)

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Lightbulb Factorial of a Number (program completed but doubt in that)

    Friends please help me out to solve this problem .....
    1 import java.io.*;
    2 class Factorial
    3 {
    4 public static void main(String args[]) throws IOException
    5 {
    6 System.out.println("Enter a value=");
    7 DataInputStream a =new DataInputStream(System.in);
    8 int b=Integer.parseInt(a.readLine());
    9 int result=frac(b);
    10 System.out.println("The factorial of "+b+" is " +result);
    11 }
    12 public static int frac(int a)
    13 {
    14 int j=a;
    15 for(int i=1;i<j;i++)
    16 {
    17 a=a*(j-i);
    18 }
    19 return a;
    20 }
    21 }
    Actually the program is perfectly running but ..
    Problem is that [B]In LIne no.-15 if i replace j by a , like for(int i=1;i<a;i++) the result was totally different (was showing output as 0) ....
    my objective is :- why it has different output thow j and a have same value .... [/B]


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Factorial of a Number (program completed but doubt in that)

    Please post your code in code tags.

    I don't understand why you're asking, "If I change my code, why does the output change?" Isn't that expected, even obvious? Can you state your question more clearly?

    Frankly, 'j' is completely unnecessary, at least the way you're using it.

    I recommend you use a Scanner object vice a DataInputStream.

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Factorial of a Number (program completed but doubt in that)

    my doubt even "a" and "j" in the block have same value but while replace "j" by "a" why the output gets changed ....

    --- Update ---

    my doubt is even "a" and "j" in the block have same value but while replace "j" by "a" why the output gets changed ....


    Quote Originally Posted by GregBrannon View Post
    Please post your code in code tags.

    I don't understand why you're asking, "If I change my code, why does the output change?" Isn't that expected, even obvious? Can you state your question more clearly?

    Frankly, 'j' is completely unnecessary, at least the way you're using it.

    I recommend you use a Scanner object vice a DataInputStream.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Factorial of a Number (program completed but doubt in that)

    Trace your code. Pick a small value of 'a', like 4 or 5, and construct a table that contains the value of each variable, i, j, and a after each for loop completes for both cases. You will see that even though j and a may start off with the same value, values change as the for loop executes, causing the results to vary.

    Exploring results you're uncertain about in this way is how you learn and understand what happens rather than have someone explain it to you in a way you may not understand, is just plain wrong, or you'll forget tomorrow because you didn't figure it out yourself.

    Good luck, keep coding, and keep being curious - but answer most of your questions yourself.

  5. #5
    Junior Member
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Factorial of a Number (program completed but doubt in that)

    1 import java.io.*;
    2 class Factorial
    3 {
    static int b;
    4 public static void main(String args[]) throws IOException
    5 {
    6 System.out.println("Enter a value=");
    7 DataInputStream a =new DataInputStream(System.in);
    8 b=Integer.parseInt(a.readLine());
    9 int result=frac(b);
    10 System.out.println("The factorial of "+b+" is " +result);
    11 }
    12 public static int frac(int a)
    13 {
    14
    15 for(int i=1;i<a;i++)
    16 {
    17 a=a*(b-i);
    18 }
    19 return a;
    20 }
    21 }


    now the output is zero why soo please tell me @Gregbranon

    --- Update ---

    1 import java.io.*;
    2 class Factorial
    3 {
    static int b;
    4 public static void main(String args[]) throws IOException
    5 {
    6 System.out.println("Enter a value=");
    7 DataInputStream a =new DataInputStream(System.in);
    8 b=Integer.parseInt(a.readLine());
    9 int result=frac(b);
    10 System.out.println("The factorial of "+b+" is " +result);
    11 }
    12 public static int frac(int a)
    13 {
    14
    15 for(int i=1;i<a;i++)
    16 {
    17 a=a*(b-i);
    18 }
    19 return a;
    20 }
    21 }


    now the output is why soo please tell me

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

    Default Re: Factorial of a Number (program completed but doubt in that)

    Please use the code tags, and please don't private message members with the same thing.

  7. The Following User Says Thank You to copeg For This Useful Post:

    arunjava (August 15th, 2013)

  8. #7
    Junior Member
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Factorial of a Number (program completed but doubt in that)

    Quote Originally Posted by GregBrannon View Post
    Trace your code. Pick a small value of 'a', like 4 or 5, and construct a table that contains the value of each variable, i, j, and a after each for loop completes for both cases. You will see that even though j and a may start off with the same value, values change as the for loop executes, causing the results to vary.

    Exploring results you're uncertain about in this way is how you learn and understand what happens rather than have someone explain it to you in a way you may not understand, is just plain wrong, or you'll forget tomorrow because you didn't figure it out yourself.

    Good luck, keep coding, and keep being curious - but answer most of your questions yourself.
    Thank your GregBrannon, thanks for your helpful advice, i shall take care of it... By the way problem is solved, there was a silly mistake ....

  9. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Factorial of a Number (program completed but doubt in that)

    I'm glad you figured it out. We often learn more by finding and correcting our own "silly" mistakes than we do by being perfect every time.

Similar Threads

  1. Replies: 10
    Last Post: November 8th, 2012, 06:29 AM
  2. Java program inputting month name and outputting month number and number of days
    By Brovahkiin501 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 19th, 2012, 09:30 AM
  3. [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
  4. Replies: 4
    Last Post: June 10th, 2009, 01:04 AM

Tags for this Thread