[Solved] How to calculate n to power m (n^m) in C#?

  

2
Topic starter

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

1 Answer
2

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);
    }
}
Share: