+ 3
Java 16.2 Need help
I need help with Java 16.2. My Code: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int number = scanner.nextInt(); int fact = number; //your code goes here while (number > 1){ fact = number * number--; number = number--; } System.out.println(fact); } }
14 Answers
+ 5
Inside the loop needs to be:
fact = fact * number; // update fact
number--; // decrease number once
+ 3
number-- decreases it by 1, number-- again decreases it once more by 1
+ 2
Only increase number once per loop iteration
+ 2
Oh yes thanks i mean fact = fact*number but yet it doesnt give any output
+ 2
Can you please put your updated code in a script on sololearn playground and link it?
+ 2
number-- works inplace,
so
number--;
or
number = number - 1;
+ 2
Oh yes yet it worked thank you very much
+ 1
But what helps me that? Because I want the number to always be lowered by one so that the program stops at the end.
+ 1
and I think it should be fact = fact*number in the loop
+ 1
in the given code it's int fact = 1; isn't it? Leave it like that...
+ 1
Yet the output is: Execution Timed out!
+ 1
Its still: Execution Timed out
+ 1
input fact = 1
then in your code
fact = fact*number;
number--;