Results 1 to 11 of 11
  1. #1
    Join Date
    Oct 2014
    Posts
    3
    Rep Power
    0

    Default Reading Nonnegative Integer and Computing it's Factorial

    Hello, this is my first post and I am very new to java, I am attempting to write a program that reads a nonnegative integer and computes and prints its factorial. So far I have:
    Java Code:
    import java.util.Scanner;
    public class Chapter3point37 {
    
        public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
       
       int nonNegative = 5;
       int count=1;
       int product=1;
       int factor=1;
       System.out.println("Input a nonnegative integer: ");
       nonNegative = input.nextInt();
       
       product = product * factor++;
       count++;
      
       while(count <= nonNegative);
       System.out.println(nonNegative + "! = " + product);
        }
        
    }
    I am struggling to figure out how I should correctly prompt the user to input the values.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Reading Nonnegative Integer and Computing it's Factorial

    You already have your input prompt. For the time being, I would suggest you assume
    the user will put in a value >= 0. However, your logic is not quite correct for computing factorials.
    The only way to learn about this is to "play" computer. Write down the values of the primary variables
    and record their values as the program executes. I will say that your while statement will not terminate
    in most cases. And it doesn't do anything useful in all cases.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Join Date
    Oct 2014
    Posts
    3
    Rep Power
    0

    Default Re: Reading Nonnegative Integer and Computing it's Factorial

    Thanks for your response Jim, could you elaborate a bit on what you mean when you say play computer? What came to my mind when I read that was that I would be inputting the values for the factorials all manually. I am getting a feeling that I do not require one of the variables I am using as well, what is your feeling on this?
    -Kind regards, Ethan

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    10

    Default Re: Reading Nonnegative Integer and Computing it's Factorial

    Hello HomeGrownIdeas, welcome.

    Jim is refereeing to debugging your program by stepping through it as the computer would. This can be done at run time through a series of print lines and watching the variables but with these few lines of code it can easily be done with a piece of paper and a pen.

    Accepting user input works fine so assume 'nonNegative' is whatever value you choose. Go through your code line by line and do what the computer would do at each step. Starting with the value 1, it won't take you too long to hit an issue.

    Regards.

  5. #5
    Join Date
    Oct 2014
    Posts
    3
    Rep Power
    0

    Default Re: Reading Nonnegative Integer and Computing it's Factorial

    Here is the most recent code I completed for this, it appears to work:
    Java Code:
    import java.util.Scanner;
    
    public class NonNegativeFactorial {
    
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
    
            int nonNegative;
            int count = 1;
            int factor = 1;
    
            System.out.println("Input a nonnegative integer: ");
            nonNegative = input.nextInt();
    
            while (nonNegative < 0) {
                System.out.println("Enter a positive integer");
                nonNegative = input.nextInt();
            }
    
            if (nonNegative > 0) {
    
                while (count <= nonNegative) {
                    factor = factor * count;
                    count++;
    
                }
                System.out.println("The factorial of the nonnegative number is " + factor);
            }
        }
    
    }
    Thanks for the replies last night, I look forward to future posts on this forum.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Reading Nonnegative Integer and Computing it's Factorial

    It looks fine (but I did not run it). I would make a couple of observations.

    1. Your input routine already guarantees that nonNegative is >= 0.
    2. By definition, 0! == 1

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Reading Nonnegative Integer and Computing it's Factorial

    !== ?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Reading Nonnegative Integer and Computing it's Factorial

    Quote Originally Posted by DarrylBurke View Post
    !== ?
    No, 0! == 1

    kind regards,

    Jos (<--- always has to explain everything overhere; always, I'm telling you)
    Build a wall around Donald Trump; I'll pay for it.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Reading Nonnegative Integer and Computing it's Factorial

    Oh, that '!'

    I thought it was the Java one

    db <- math impaired
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Reading Nonnegative Integer and Computing it's Factorial

    Well, I thought when I typed it in I should use 0! = 1. But then folks, would say, duh!!, of course 0 is not equal to 1.
    And then I would have to spend the rest of my life trying to save face.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Reading Nonnegative Integer and Computing it's Factorial

    It's all Darryl' fault: he uses an incorrect tokenizer, so he jumps to incorrect conclusions. It's also K&R's fault: they should've left the inequality operator to what it was in other languages: 0 <> 1 and 0! == 1; so basically it's all Java's fault: it should've corrrected all previous mistakes ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 2
    Last Post: 06-22-2013, 08:49 PM
  2. Replies: 1
    Last Post: 11-11-2012, 02:55 AM
  3. Reading/Writing Integer Bytes
    By AndrewM16921 in forum New To Java
    Replies: 1
    Last Post: 04-04-2011, 12:56 AM
  4. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 01:50 AM
  5. Factorial
    By Anindo in forum New To Java
    Replies: 4
    Last Post: 07-28-2009, 10:46 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
  •