settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: How to calculate n to power m (n^m) in C#?

+3 votes

I need a program in C# which is calculating n to power m (or n^m). Please help me write it.

asked in C# category by user hues

1 Answer

+1 vote
 
Best answer

Use for loop:

using System;

class ForLoop
{
    static void Main()
    {
        Console.Write("n = ");
        int n = int.Parse(Console.ReadLine());
        Console.Write("m = ");
        int m = int.Parse(Console.ReadLine());
        decimal result = 1;
        for (int i = 0; i < m; i++)
        {
            result *= n;
        }
        Console.WriteLine("n^m = " + result);
    }
}


 


 

console application c sharp

answered by user mitko
edited by user golearnweb
...