initialize i and j in for loop

FranK Duc 121 Reputation points
2021-08-25T12:08:34.523+00:00

Hello,

I am trying to calculate a binomial:

 int X;
    int Y;
    int n = Convert.ToInt32(Console.ReadLine());   // number of horses    6
    int p = Convert.ToInt32(Console.ReadLine());   // number of try           2
    int i = 1;
    int k = 1;

    Console.WriteLine(" Entrez le nombre de cheveux partants:");
    Console.WriteLine(" Entrez le nombre de cheveux joués:");

      for (i, k; i < n && k < p; n++, k++  )

     {
      X *= i  / (i - k); 
      Y *= i  / (k  * (i – k) );

     }

      Console.WriteLine(" Dans l’ordre, une chance sur :{0}", X);
      Console.WriteLine(" Dans le désordre, une chance sur :{0}", Y);

I get an error: only assigment in my for loop.
Is there a way i can use two variables in the same loop?

Thank you

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,249 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 26,201 Reputation points
    2021-08-25T13:15:27.48+00:00

    The logic for factorial is fairly basic. Below is a lambda but the logic could also be a utility function.

    Func<int, int> factorial = (n) =>
    {
        int res = 1;
        while (n != 1)
        {
            res = res * n;
            n = n - 1;
        }
        return res;
    };
    
    int results = factorial(5);
    Console.WriteLine(results);
    

    I am trying to calculate a binomial

    Are you trying to graph a binomial? Or solve for X and Y where they cross an axis?


1 additional answer

Sort by: Most helpful
  1. AgaveJoe 26,201 Reputation points
    2021-08-25T12:36:07.663+00:00

    The C# for...loop reference documentation explains and illustrates the for...loop usage.

    The following code runs but the logic does not make sense.

    int X = 0;  
    int Y = 0;  
    int n = Convert.ToInt32(Console.ReadLine());   // number of horses    6  
    int p = Convert.ToInt32(Console.ReadLine());   // number of try           2  
    int i = 1;  
    int k = 1;  
      
    Console.WriteLine(" Entrez le nombre de cheveux partants:");  
    Console.WriteLine(" Entrez le nombre de cheveux joués:");  
      
    for (i = 0, k = 0; i < n && k < p; n++, k++)  
    {  
        X *= i / (i - k);  
        Y *= i / (k * (i - k));  
    }  
      
    Console.WriteLine(" Dans l’ordre, une chance sur :{0}", X);  
    Console.WriteLine(" Dans le désordre, une chance sur :{0}", Y);  
    
    1 person found this answer helpful.