For loop with a mathemtical expression involved

I am trying to utilize the Math.Cos() function in c# to print a range of of values from 0 to 2pi increasing by .1pi, so 20 iterations. The problem is I cannot get my x value to change as the for loop is executing.

    public void Cos()
    {
        double x = 0;
        double a = Math.PI * x;
        double b = Math.Cos(a);
    for (int i = 0; i < 21; i++)
    {

        Console.WriteLine("Cos({0})pi = {1}", x, b);
        x += .1;

    }
}

When I print the results to the console, it only remembers the value of cos at x = 0. So I just get 1, 20 times as a result for Cos(.1)pi, Cos(.2)pi, etc…

#c-sharp #loops

4 Likes1.85 GEEK