+ 1

How do I get the "for" loop to give me one outcome?

hello! I am facing a problem within the beginner course of C#. You see, in the "for loops" lesson, the practice at the end requires you to write a code that calculate and output the factorial of an input. I did try many combinations. This is my last: using System; public class Program { static void Main(string[] args) { int num = Convert.ToInt32(Console.ReadLine()); //your code goes here for (int mercy=num--;mercy>1;mercy*=num--) Console.WriteLine(mercy); } } This one does grant me the correct final answer. Yet, being a loop, it does output all the other numbers prior to the answer. This, consequently, fail the test since it only wants the final outcome. I checked the lesson again and it doesn't show anything to help with this. In fact, it only has examples that does output all the numbers of the loop. I am stuck, I can't advance this way. If someone knows how to get me out of this. Please help! Thank you in advance.

17th Sep 2024, 11:23 AM
Chaimae Saadi
Chaimae Saadi - avatar
1 ответ
+ 6
Create a variable int mercy = 1 Iterate the numbers 1 to num using a for-loop. On each iteration, multiply mercy with the current number. After the loop, output mercy.
17th Sep 2024, 11:41 AM
Lisa
Lisa - avatar