Question Loop with multiplication numbers

Rabi

Member
Joined
Mar 22, 2022
Messages
17
Programming Experience
Beginner
this code to multiplication numbers fro 6 to 1 like this : 6*5*4*3*2*1 = 720
the problem is the result is : 0
C#:
 int sum = 0;
             int n = 6;
            for (int x = n; x > 0; x--)
            {
                 
                    sum *= x;
            }     textBox2.Text = sum.ToString();    // print : 0
 
Last edited by a moderator:
Solution
Debug your code, as you should have before posting, and the issue will be obvious. You should not be posting a question about code that you have not debugged, i.e. set a breakpoint and stepped through while examining the state.

By the way, "sum" is the result of an addition. The result of a multiplication is the "product". That might actually be part of the problem, i.e. you adapted code from addition to multiplication without thinking it through. If you want to add to a number without changing it, you add zero, right? If you want to multiply a number without changing it, what do you do? This is kiddie maths logic, not programming. This is why you need to work out the logic BEFORE you write the code, then write code to implement that...
Debug your code, as you should have before posting, and the issue will be obvious. You should not be posting a question about code that you have not debugged, i.e. set a breakpoint and stepped through while examining the state.

By the way, "sum" is the result of an addition. The result of a multiplication is the "product". That might actually be part of the problem, i.e. you adapted code from addition to multiplication without thinking it through. If you want to add to a number without changing it, you add zero, right? If you want to multiply a number without changing it, what do you do? This is kiddie maths logic, not programming. This is why you need to work out the logic BEFORE you write the code, then write code to implement that logic.
 
Solution
Back
Top Bottom