C# Program To Find Power of a number using for Loop - C# Program

C# Program To Find Power of a number using for Loop

Two numbers are entered through the keyboard. Write a program to find the value of one number raised to the power of another.

using System;
namespace PowerNumberApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b;
            double  pnum = 1;
            Console.Write("Enter the value of A :");
            a = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter the value of B :");
            b = Convert.ToInt32(Console.ReadLine());
            if (b > 0)
            {
                for (int i = b; i>0; i--)
                    pnum = pnum * a;
            }
            else if(b <0)
            {
                for(int i=b; i<0; i++)
                    pnum = pnum/a;
            }

            Console.WriteLine("{0} Raised to the Power of {1} is {2}", a, b, pnum);

            Console.ReadKey();

        }
    }
}

Output - C# Program To Find Power of a number using for Loop

Enter the value of A : 10
Enter the value of B : 3

10 Raised to the Power of 3 is 1000

C# Program To Find Power of a number using for Loop