0

who can help me with this exercise? solution + explanation.

For your math class you need a program to calculate the factorial of a number. You're given a program which takes a number as input. Task Complete the program to calculate the factorial of the given number and output it. Sample input 6 Sample output 720 Explanation The factorial of a number is equal to the product of all  numbers less than or equal to the given number. The factorial of 6 will be  6*5*4*3*2*1 = 720. Hint Use while loop to calculate the factorial of the number. 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 = 1;             //your code goes here                 } }

2nd Dec 2021, 7:04 AM
Daniel-Razvan Dumitrascu
Daniel-Razvan Dumitrascu - avatar
5 Respuestas
+ 4
The key to solving this problem is understanding the use of loop, especially the while loop (mentioned in hint). I would recommend you to search the forum for questions bearing similar topic (same coding problem). You may find inspirations from looking at others' suggestions.
2nd Dec 2021, 8:16 AM
Ipang
+ 3
Daniel-Razvan Dumitrascu You have a variable called fact, already given with a value of 1. Set up your while loop as per the suggestion of Ipang May I suggest while number > 0 as your defining statement. You will need to adjust the fact variable by using the *= operator You will also need to decrement the loop by 1 each iteration. Hopefully, you now have enough hints to write your code. Review the sections I have mentioned in your tutorial
2nd Dec 2021, 8:33 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Loop i=6 sum=sum*i i--
2nd Dec 2021, 8:37 AM
zemiak
+ 1
For loop, the loop occurs if the int n=1 is not equal or <= to given number, ++n to change value of n, the loop occurs and inside the loop fact=fact*n, to reverse the loop for much proper then change the logic.
3rd Dec 2021, 9:59 AM
John Michael Guadalquiver Donceras
John Michael Guadalquiver Donceras - avatar
0
I want to mention that I can't figure out how to make them multiply automatically.
2nd Dec 2021, 7:05 AM
Daniel-Razvan Dumitrascu
Daniel-Razvan Dumitrascu - avatar