-3

My Java code should let the user enter a number and then calculate the factorial of that number and I need to use "for loop" When I enter number 5 it tells me the factorial is 6 when it should be 120. I have tried to watch tutorials with factoring loop but they wont work, i think its because i have "do" command that gets values from calling

Here is the code:

static Scanner kboard = new Scanner(System.in); //variable to read in values

public static void main(String[] args) {
  int choice = 0;
  String dummy = "";
  String forename = "";
  String surname = "";
  int number = 0;



  do {

    System.out.println("1. display the user name, 2. calculate factorial, 3. exit");
    choice = kboard.nextInt();
    dummy = kboard.nextLine(); //strips out the return 

    if (choice == 1) {
      forename = getforename();
      surname = getsurname();
      displaydetails(forename, surname);
    }

    if (choice == 2) {
      number = getnumber();
      calcfactorial(number);
    }

    if (choice == 3) {
      System.out.println("bye");
    }

  } while (choice != 3);
}


public static String getforename() {

  String newforename = "";

  System.out.println("Please enter your forename ?");
  newforename = kboard.next();
  return (newforename);
} // end getforename

public static String getsurname() {
  /*
  Code to prompt the user to enter a surname
  */
  String newsurname = "";

  System.out.println("Please enter your surname ?");
  newsurname = kboard.next();
  return (newsurname);
} // end getsurname


public static void displaydetails(String displayforename, String displaysurname) {
  /*
  Code will carry out prescribed changes and display the result
  */
  char displayinitial;
  String displayusername = "";

  displaysurname = displaysurname.toUpperCase();
  displayinitial = displayforename.charAt(0);
  displayusername = displayinitial + displaysurname;
  System.out.println("Your username is " + displayusername);

}



public static int getnumber() {


  System.out.println("What numbers factorial do you want to know?");
  int newnumber = kboard.nextInt();
  return newnumber;
}

public static void calcfactorial(int newnumber) {
  int count = 0;
  int factorial = 1;

  if (newnumber > 0) {
    for (count = 1; count <= newnumber; count++); {

      factorial = factorial * count;
      System.out.println("Factorial of " + newnumber + " is: " + factorial);

    }

  } else {
    System.out.println("Number must be positive");

  }

}
3
  • 2
    This would be useful: stackoverflow.com/questions/25385173/… Commented Dec 3, 2018 at 20:54
  • Another tip: use editor which can indent your code for you. This way you can notice things like lack of { } and extra ; where they are not needed (like right after if(condition); or lops while(conidtion);{...} for(...);{...}). Commented Dec 3, 2018 at 21:01
  • Related: Why do java if statement fail when it ends in semicolon (also applies to loops). Commented Dec 3, 2018 at 21:06

2 Answers 2

6

If you had used a debugger, then you could tell that it's only executing the multiplication in the calcfactorial method once, and count is already 6 at this point. Reasons:

First, remove the semicolon at the end of the for condition loop. It is acting as the body of the for loop. This makes count equal to newnumber + 1, or 6.

Second, move your print statement after the end of the for loop, but still within the if block. Otherwise you'll get newnumber lines of printouts.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! Im still new to java and eclipse so im sorry if i did something stupid... I dont know why my question got downvoted :( But thank you so much man!
-1

I am not going to fully give away the answer...user azurefrog posted a helpful link on debugging code.

However, your for loop is doing something you don't intend:

public static void  calcfactorial(int newnumber)
{
    int count = 0;
    int factorial = 1;

    if (newnumber > 0)
    {
        for (count=1;count<=newnumber;count++);
        {

            factorial = factorial * count; System.out.println("Factorial of "+newnumber+" is: "+factorial);

        }

    }

    else
    {
        System.out.println("Number must be positive");

    }

}

Reading through this, the line factorial = factorial * count; is just going to do 1*1, 1*2, 1*3, 1*4, 1*5, etc. for whatever is entered to be calculated for factorial. This is not the correct logic.

I recommend thinking through the logic of the for loop a bit more carefully. You should be using the number entered (i.e. 5 for the example you've given) somewhere in that loop.

2 Comments

You have an error in your for loop code. A decent IDE would of shown you that.
Ha - I copied from the original and never stuck it in an IDE. Guessing OP missed a brace. Thanks for the snide remark though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.