Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Globalization;
- using System.Threading;
- class CalculateIntegerByPowerSbyte
- {
- static void Main()
- {
- Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
- checked
- {
- int numX = int.Parse(Console.ReadLine());
- sbyte powY = sbyte.Parse(Console.ReadLine());
- decimal result = CalculateIntegerXByPowerY(numX, powY);
- Console.WriteLine(result);
- }
- }
- static decimal CalculateIntegerXByPowerY(int numX, sbyte powY)
- {
- /* This method calculated result from integer X by power sbyte Y: (X^Y).
- * He is the best for non-bigger integers for numX and
- * very small whole numbers for power Y.
- * If you must use bigger numbers, you need modify it.
- * Modify return variable (result) type to double or BigInteger,
- * or use System.Math.Pow(numX, powY), wich is too slower! */
- checked
- {
- decimal result = 0;
- if (numX != 0)
- {
- result = 1;
- if (powY > 0)
- {
- for (int pow = 0; pow < powY; pow++)
- {
- result *= numX;
- }
- }
- else if (powY < 0)
- {
- powY = Math.Abs(powY);
- for (int pow = 0; pow < powY; pow++)
- {
- result *= numX;
- }
- result = 1.0m / result;
- }
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement