Exercise 1 of Lesson 4 contains some redundant code

https://github.com/NordicDeveloperAcademy/ncs-fund/blob/main/v2.9.0-v2.7.0/l4/l4_e1_sol/src/main.c

The current logic uses i and j to calculate factorial.

	for (i = 1; i <= MAX_NUMBER_FACT; i++) {
		factorial = 1;
		for (j = 1; j <= i; j++) {
			factorial = factorial * j;
		}
		printk("The factorial of %2d = %ld\n", i, factorial);
	}

But actually, it could be done with only i. j is not needed in this case.
I could not understand why j is needed in the second for loop here.

	for (i = 1; i <= MAX_NUMBER_FACT; i++) {
		factorial = factorial * i;
		printk("The factorial of %2d = %ld\n", i, factorial);
	}

The output of factorial is the same.

Parents Reply Children
Related