Answer to Question #272283 in C# for chi

Question #272283

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


1
Expert's answer
2021-11-29T02:53:34-0500
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;
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

reyhan
03.12.21, 05:08

thanks alot of information

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS