I'm trying to multiplicate 4 with its sub numbers, meaning 4x3x2x1 så 24 should be printed but its not. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I'm trying to multiplicate 4 with its sub numbers, meaning 4x3x2x1 så 24 should be printed but its not.

import java.util.* ; class Main { public static void main(String[] args) { int i = 4 ; for(;i > 0 ;i--){ System.out.println(i*(i-1)); } } }

17th Sep 2022, 10:53 AM
Lenoname
5 Answers
+ 3
I understood you, but you need to derive the result of all multiplication at the end, the final. so with this approach, you need to store the result of intermediate calculations. otherwise, each iteration of the loop will reset the calculation data to zero each time
17th Sep 2022, 12:33 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
Yaroslav Vernigora Idk, i just want to use a loop to calculate it, like 4x(4-1)x(4-2)x(4-3)
17th Sep 2022, 11:48 AM
Lenoname
+ 1
You are priting subresults evericycle. You have to store subresults to somevhere, and in the end prind final result.
18th Sep 2022, 2:04 PM
Filip Dobeš
+ 1
public class Factorial { public static void main(String[] args) { // Define variable int num = 4; // Call the function recursively long fact = calcFact(num); // Print result System.out.println("Factorial for " + num + " is " + factorial); } public static long calcFact(int n) { if (n >= 1) return n * calcFact(n - 1); else return 1; } }
18th Sep 2022, 6:09 PM
Gábor Beszedics
Gábor Beszedics - avatar
0
Hi! in fact, you need to calculate the factorial of the number. do you need to solve the problem recursively?
17th Sep 2022, 11:36 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar