write a program that calculates the power value of the input base number and exponent number. Apply three solutions using the three looping statements: Sample input/output dialogue Enter base no. 5 Input data Enter exponent no. 3 Second input data Power value: 125 Output value (The computation is: 53 = 5 * 5 * 5 = 125)
1st Solution – using for loop
2nd Solution –using while loop
3rd Solution-‐ using do while loop
using System;
using System.Collections.Generic;
namespace App
{
class Program
{
public static void Main()
{
Console.Write("Enter base no.: ");
int baseNo = int.Parse(Console.ReadLine());
Console.Write("Enter exponent no.: ");
int exponentNo = int.Parse(Console.ReadLine());
Console.WriteLine("Power value (using - for loop): {0}", ComputePowerWithFor(baseNo, exponentNo));
Console.WriteLine("Power value (using - while loop): {0}", ComputePowerWithWhile(baseNo, exponentNo));
Console.WriteLine("Power value (using - do while loop): {0}", ComputePowerWithDoWhile(baseNo, exponentNo));
Console.ReadLine();
}
private static int ComputePowerWithDoWhile(int baseNo, int exponentNo)
{
int power = 1;
int i = 0;
do
{
power *= baseNo;
} while (++i < exponentNo);
return power;
}
private static int ComputePowerWithWhile(int baseNo, int exponentNo)
{
int power = 1;
int i = 0;
while (++i < exponentNo)
{
power *= baseNo;
}
return power;
}
private static int ComputePowerWithFor(int baseNo, int exponentNo)
{
int power = 1;
for (int i = 0; i < exponentNo; i++)
{
power *= baseNo;
}
return power;
}
}
}
Comments
thanks alot of information
Leave a comment