1. Member
Join Date
Oct 2014
Posts
3
Rep Power
0

## 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. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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

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

## 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. Senior Member
Join Date
Oct 2010
Posts
393
Rep Power
10

## 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. Member
Join Date
Oct 2014
Posts
3
Rep Power
0

## 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. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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.

2. By definition, 0! == 1

Regards,
Jim

!== ?

db

8. ## Re: Reading Nonnegative Integer and Computing it's Factorial

Originally Posted by DarrylBurke
!== ?
No, 0! == 1

kind regards,

Jos (<--- always has to explain everything overhere; always, I'm telling you)

9. ## Re: Reading Nonnegative Integer and Computing it's Factorial

Oh, that '!'

I thought it was the Java one

db <- math impaired

10. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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

11. ## 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

#### Posting Permissions

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