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 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:
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:
- Write A Program To Find Numbers Above And Below The Average In C#
- Write A C# Program To Print The Multiplication Table Of A Number
- Write a C# Program to Print All Natural Numbers in Reverse Order
- Write a C# Program to Print the Factorial of a Number
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…