settingsAccountsettings
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);
    }
}


 


 

calcualte n to power of m in c sharp in visual basics in console application

answered by user mitko
edited by user golearnweb
...