import java.util.*;
public class factorial_recursion
{
public static int factorial(int user_input)
{
while (user_input >=0)
{
if (user_input == 1)
return 1;
else
return user_input*(factorial(user_input-1));
}
return factorial(user_input);
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a positive integer: ");
int user_input = input.nextInt();
int x = factorial(user_input);
System.out.println(user_input + " factorial is " + x);
}
}
3 Replies - 560 Views - Last Post: 30 October 2011 - 09:55 AM
#1
how to have program continually require input unless negative
Posted 29 October 2011 - 01:28 PM
I know its a simple solution of implementing a while loop I believe, but I cannot figure out where to write it. The goal is to have the program keep asking for user input and then print the factorial until the user inputs a negative in which case the program exits. The code currently calculates a single user input and prints it then terminates.
Replies To: how to have program continually require input unless negative
#2
Re: how to have program continually require input unless negative
Posted 29 October 2011 - 01:33 PM
You want to use a while loop in your user input section, not the factorial() method. Either use recursion or a loop to calculate the factorial of the param, but not both.
public static int factorial(int x){
//calculate x!
}
public static void main(String[] args){
int input = 0;
int fact = 0;
do{
input = /*get input*/;
fact = factorial(input);
}while(input >= 0);
}
#3
Re: how to have program continually require input unless negative
Posted 29 October 2011 - 02:40 PM
You seem to have a problem understandig the scope of a variable
user_input exists as parameter of the method and then vanishes
The while statement is completly useless, it will be excuted only once
If you have a user_input < 0 you have a infinite loop
because of the useless while it could be written
but it does not solve your infine loop
actually the concept is even easier
Happy coding
user_input exists as parameter of the method and then vanishes
The while statement is completly useless, it will be excuted only once
If you have a user_input < 0 you have a infinite loop
public static int factorial(int user_input)
{
while (user_input >=0) // if user_input >= 0 enter the while
{
if (user_input == 1) // if user_input == 1
return 1; // return and exit the while
else // in all other cases
return user_input*(factorial(user_input-1)); // return and exit the while anyhow
}
// ok user_input < 0
return factorial(user_input); // return factorial(that number < 0
}
because of the useless while it could be written
public static int factorial(int user_input)
{
if(user_input >=0) // if user_input >= 0 enter the while
{
if (user_input == 1)
return 1;
else
return user_input*(factorial(user_input-1));
}
return factorial(user_input);
}
but it does not solve your infine loop
actually the concept is even easier
public static int factorial(int user_input)
{
if(user_input <= 1)
return 1;
return user_input * factorial(user_input - 1);
}
Happy coding
#4
Re: how to have program continually require input unless negative
Posted 30 October 2011 - 09:55 AM
You need a loop that's checked at the top:
int user_input = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter a positive integer (or otherwise to quit): ");
while (input.hasNextInt() && ((user_input = input.nextInt()) > 0)) {
int x = factorial(user_input);
System.out.println(user_input + " factorial is " + x);
}
Page 1 of 1

New Topic/Question
Reply


MultiQuote






|