+ 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); } }

25th Jan 2022, 6:55 PM
Dvid313
Dvid313 - avatar
14 odpowiedzi
+ 5
Inside the loop needs to be: fact = fact * number; // update fact number--; // decrease number once
25th Jan 2022, 8:11 PM
Lisa
Lisa - avatar
+ 3
number-- decreases it by 1, number-- again decreases it once more by 1
25th Jan 2022, 7:05 PM
Lisa
Lisa - avatar
+ 2
Only increase number once per loop iteration
25th Jan 2022, 6:57 PM
Lisa
Lisa - avatar
+ 2
Oh yes thanks i mean fact = fact*number but yet it doesnt give any output
25th Jan 2022, 7:15 PM
Dvid313
Dvid313 - avatar
+ 2
Can you please put your updated code in a script on sololearn playground and link it?
25th Jan 2022, 7:28 PM
Lisa
Lisa - avatar
+ 2
number-- works inplace, so number--; or number = number - 1;
25th Jan 2022, 8:18 PM
Lisa
Lisa - avatar
+ 2
Oh yes yet it worked thank you very much
25th Jan 2022, 8:19 PM
Dvid313
Dvid313 - avatar
+ 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.
25th Jan 2022, 7:03 PM
Dvid313
Dvid313 - avatar
+ 1
and I think it should be fact = fact*number in the loop
25th Jan 2022, 7:10 PM
Lisa
Lisa - avatar
+ 1
in the given code it's int fact = 1; isn't it? Leave it like that...
25th Jan 2022, 7:17 PM
Lisa
Lisa - avatar
+ 1
Yet the output is: Execution Timed out!
25th Jan 2022, 7:20 PM
Dvid313
Dvid313 - avatar
25th Jan 2022, 8:09 PM
Dvid313
Dvid313 - avatar
+ 1
Its still: Execution Timed out
25th Jan 2022, 8:15 PM
Dvid313
Dvid313 - avatar
+ 1
input fact = 1 then in your code fact = fact*number; number--;
27th Jan 2022, 3:16 PM
Hirdesh Prajapati
Hirdesh Prajapati - avatar