I need a program that will return an integers factorial, but only when the number entered is between 0 and 20. I have the code to disregard anything below 0 and above 20, but something with my math and/or syntax is off and I dont fully comprehend how to fix the error. My code is below....

import java.util.Scanner;

public class Factorial {

    public static void main(String[] args) {
        int num; // number to find factorial of
        long fac; // factorial so far
        Scanner scan = new Scanner(System.in);

        // Get value to compute factorial of
        System.out.print("Please enter a non-negative integer: ");
        num = scan.nextInt();

        // Compute and print factorial
        // your code here...
        while(num < 0 || num > 20){
            System.out.println("Factorial is undefined for negative or large numbers");
            System.out.print("Please enter an integer number between 0 and 20: ");
            num = scan.nextInt();
        }
//        while(num > 0 || num < 20) {
//            for (int i = 1; i < 50; i ++){
//            fac = num(i) * num(i -1);
//            }
for (int i = 1 ; i <= 20; i++){
num=num - 1 * i;
}
System.out.println("The factorial is: " + num);

    }
  }
}

Dani AI

Generated

For : quick diagnostic first — three things are wrong in the posted code that explain the wrong result.

  • You modify the input (num = num - 1 * i) instead of using a separate accumulator. That expression is subtraction (operator precedence makes it num - (1*i)), so it repeatedly reduces num rather than producing a product.
  • Your loop iterates to 20 every time (i <= 20) instead of iterating up to the user’s num.
  • The factorial accumulator fac is never given an initial value nor used when printing.

Correct approach (conceptually): use a separate accumulator initialized to 1, multiply it by each integer from 2 up to num, and print the accumulator. For num == 0 the factorial is 1. The reason your program limits input to 0..20 is practical: 20! = 2432902008176640000 fits in a signed 64-bit long, but 21! will overflow. If you need larger factorials use java.math.BigInteger.

A compact, robust example (input validation + correct accumulation):

import java.util.Scanner;

public class Factorial {
  static long factorial(int n) {
    long acc = 1L;
    for (int i = 2; i <= n; i++) acc *= i;
    return acc;
  }

  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.print("Enter integer 0..20: ");
    while (!scan.hasNextInt()) { scan.next(); System.out.print("Enter integer 0..20: "); }
    int num = scan.nextInt();
    while (num < 0 || num > 20) { System.out.print("Out of range. Enter 0..20: "); num = scan.nextInt(); }
    System.out.println("The factorial is: " + factorial(num));
    scan.close();
  }
}

Notes and quick tips: was right to suggest using a separate variable for the product; pointed toward a loop-based solution but missed initializing the accumulator; ’s point about explaining changes rather than dumping code is sound — initialize the accumulator, multiply (not subtract), loop to num, and print the accumulator. If users want values beyond 20!, compute with BigInteger using the same loop pattern.

Recommended Answers

All 4 Replies

in your loop at line 25 you dont need to iterate it 20 times but you use the input instead, use another variable to store the factorial

So you are saying where I have I <= 20, I should have my user input as "num <= 20" and use the variable fac as my factorial variable?

you need to update the code at line25,26,27,28 as

if(num ==0)
{
 fac = 1;
}
else{
 for(int i=1;i<=num;i++){
  fac = fac*num
 }
}
System.out.println("The factorial is: " + fac);

``

@piyush
Please don't just spoon feed raw code to people. All that teaches them is how easy it is to cheat with a quick copy/paste. You didn't even attempt to explain why you made the changes you did. Give people assistance that helps them learn how to fix problems for themselves.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.