I’m having difficulties with a question from a past exam paper. I’m trying to multiply a from number to a n number. In other words: from*(from+1)(from+2)…*n.
I need to solve this problem by using a while loop. I have done this so far and not sure what to do. I know the code is wrong but have been stuck for a while.
public class Fact {
public int last;
private int factPartND(final int from, final int n) {
int fromNum = from;
int toNum = n;
int result = 1;
int c = 1;
while (fromNum <= toNum) { // e.g.5*6*7*8*9*10*11
result = (fromNum) * (fromNum + c); // calculate 5*6
final int temp = result; // store 5*6
int result1 = temp * (fromNum + c); // store 5*6*7*....
c++; // increments the fromNum in the while code
fromNum++; // increments 5 to 11 in the while condition
last = result1;
}
return last;
}
public static void main(String[] args) {
Fact f = new Fact();
System.out.println(test);
}
}
Strictly
while: