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)); } } }
5 ответов
+ 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
+ 1
Yaroslav Vernigora Idk, i just want to use a loop to calculate it, like 4x(4-1)x(4-2)x(4-3)
+ 1
You are priting subresults evericycle. You have to store subresults to somevhere, and in the end prind final result.
+ 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;
}
}
0
Hi! in fact, you need to calculate the factorial of the number. do you need to solve the problem recursively?