How exactly do I solve this? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How exactly do I solve this?

I am trying to do the practice exercise at the end of the For loop section of Introduction to C#. But I am confused as hell on the right code to perform the task it wants. The factorial of a number N is equal to 1 * 2 * 3 * ... * N For example, the factorial of 5 is 1 * 2 * 3 * 4 * 5 = 120. The given program takes a number from the input. Task Create a program to calculate and output the factorial of that input number.

17th Jul 2023, 1:46 AM
James Welsh
2 Answers
+ 3
You could try it with recursion, I am not familiar with C# sytantx but a sudo code might help you. //Sudo code: Int fact=1; Function factorial(int a){ fact=a*factorial(a-1) }
17th Jul 2023, 3:26 AM
Yasin Rahnaward
Yasin Rahnaward - avatar
+ 2
James Welsh start with the result variable initialized to 1. Use a for loop that increments the loop index from 1 through N. Inside the loop, multiply the result value by the loop index and save it back into the result variable. After the loop, print the result to the console.
17th Jul 2023, 1:58 PM
Brian
Brian - avatar