how to use a power in c# without math code example

Example 1: math.pow in C# using loop

static float CalculatePower(float x, int y)
        {
            float temp;
            if (y == 0)
                return 1;
            temp = CalculatePower(x, y / 2);
            if (y % 2 == 0)
                return temp * temp;
            else
            {
                if (y > 0)
                    return x * temp * temp;
                else
                    return (temp * temp) / x;
            }
        }

Example 2: how to makee power un loops c#

using System;using System.Collections.Generic;using System.Linq;using System.Text; public class csharpExercise{    static void Main(string[] args)    {         int basenumber, exponent,power, i;         // Reading number        Console.Write("Enter any number: ");        basenumber = Convert.ToInt32(Console.ReadLine());         Console.Write("Enter any number: ");        exponent = Convert.ToInt32(Console.ReadLine());         power = 1;        i = 1;        //caculatinh power of given number        while (i <= exponent)        {            power = power * basenumber;            i++;        }        Console.Write("Power : "+ power);         Console.ReadLine();    }}

Tags:

Java Example