21 January 2014

Power function in C#

using System;

namespace SamplePrograms
{
    class PowerFunction
    {
        public static void Main()
        {
            Console.WriteLine("Enter your base :");
            int Base = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter your exponent :");
            int Exponent = Convert.ToInt32(Console.ReadLine());
            int Result = Power(Base, Exponent);
            Console.WriteLine("Result = {0}", Result);
        }
      public static int Power(int Base, int Exponent)
        {
            int Result = 1;
            for (int i = 1; i <= Exponent; i++)
            {
                Result = Result * Base;
            }
            return Result;
        }
    }
}

No comments:

Post a Comment