CSharp - Write program to Raise the Power


You will write a program to calculate the nth power of the number x given the decimal x and the positive integer n on its input.


You can repeat the multiplication by x.


using System;
class Program/*w w  w .j  a  v  a 2s.c  o  m*/
    static void Main(string[] args)
        // Inputs 
        Console.Write("Enter x (number to be raised): ");
        string inputX = Console.ReadLine();
        double x = Convert.ToDouble(inputX);

        Console.Write("Enter n (power): ");
        string inputN = Console.ReadLine();
        int n = Convert.ToInt32(inputN);

        // Calculating 
        double result = 1;
        for (int count = 0; count < n; count++)
            result *= x;

        // Output 
        Console.WriteLine("x^n=" + result.ToString());