Write C# Program To Calculate Power Using While & For Loop

Do you want to write a program to calculate power in C#? In this C# tutorial, I will explain how to write a C# program to calculate power using while & for loop.

To calculate the power of a number in C# using while and for loops, initiate a while loop or a for loop to multiply the base number by itself repeatedly, as many times as specified by the exponent. Both loops maintain a counter that determines the number of multiplications, effectively computing the exponential value.

The power of a number refers to how many times a number (the base) is multiplied by itself. The operation is denoted as an, where a is the base and n is the exponent. For instance, 23 (2 raised to the power of 3) equals 8, since 2×2×2=82×2×2=8.

Calculate Power Using While Loop in C#

We can use a while loop to calculate power in C#. The while loop is ideal for situations where the number of iterations isn’t known beforehand. Here’s how you can use it for power calculation in C# using a while loop.

using System;

class PowerCalculator
{
    static void Main()
    {
        Console.WriteLine("Enter base number:");
        int baseNumber = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Enter exponent:");
        int exponent = Convert.ToInt32(Console.ReadLine());

        long result = 1; // long is used to handle larger results
        int i = 0;

        while (i < exponent)
        {
            result *= baseNumber;
            i++;
        }

        Console.WriteLine($"{baseNumber}^{exponent} = {result}");
    }
}

In this code, the while loop continues multiplying the base number until the counter i reaches the exponent. After running the code using a console application in C#, you can see the output in the screenshot below:

write c# program to calculate power using while loop

Write C# Program To Calculate Power Using For Loop

Now, let us see how to write a C# program to calculate power using a for loop. A for loop is a more concise alternative, especially when the number of iterations is known, as in the case of power calculation. Here is the complete code:

using System;

class PowerCalculator
{
    static void Main()
    {
        Console.WriteLine("Enter base number:");
        int baseNumber = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Enter exponent:");
        int exponent = Convert.ToInt32(Console.ReadLine());

        long result = 1;

        for (int i = 0; i < exponent; i++)
        {
            result *= baseNumber;
        }

        Console.WriteLine($"{baseNumber}^{exponent} = {result}");
    }
}

Once you run the code using Visual Studio, you can see the output in the screenshot below:

write c# program to calculate power using for loop

This is how to write a c# program to calculate power using a for loop.

Conclusion

In this C# tutorial, I have explained how to write a write c# program to calculate power using while & for loop. We have executed two separate programs for:

  • Write a C# program to calculate the power using a while loop.
  • Write a C# program to calculate the power using a for loop.

You may also like: