Programming Fundamentals/Loops/C Sharp: Difference between revisions

From Wikiversity
Jump to navigation Jump to search
Content deleted Content added
Creating
(No difference)

Revision as of 03:37, 8 February 2017

loops.cs

// This program displays a temperature conversion table showing Fahrenheit
// temperatures from 0 to 100, in increments of 10, and the corresponding 
// Celsius temperatures using While, For, and Do loops.

using System;

public class MainClass
{
    public static void Main(String[] args)
    {
        WhileLoop();
        ForLoop();
        DoLoop();
    }

    private static void WhileLoop()
    {
        double f;
        double c;
        
        Console.WriteLine("F°    C°");
        f = 0;
        while (f <= 100)
        {
            c = (f - 32) * 5 / 9;
            Console.WriteLine(f.ToString() + " = " + c.ToString());
            f = f + 10;
        }
    }

    private static void ForLoop()
    {
        double f;
        double c;
        
        Console.WriteLine("F°    C°");
        for (f = 0 ; f <= 100 ; f += 10)
        {
            c = (f - 32) * 5 / 9;
            Console.WriteLine(f.ToString() + " = " + c.ToString());
        }
    }

    private static void DoLoop()
    {
        double f;
        double c;
        
        Console.WriteLine("F°    C°");
        f = 0;
        do
        {
            c = (f - 32) * 5 / 9;
            Console.WriteLine(f.ToString() + " = " + c.ToString());
            f = f + 10;
        } while (f <= 100);
    }
}

Try It

Copy and paste the code above into one of the following free online development environments or use your own C Sharp compiler / interpreter / IDE.

See Also