Write C# program to calculate power using while & for loop


Introduction

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Calculating power of a number using while loop


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. public class csharpExercise
  7. {
  8. static void Main(string[] args)
  9. {
  10.  
  11. int basenumber, exponent,power, i;
  12.  
  13. // Reading number
  14. Console.Write("Enter any number: ");
  15. basenumber = Convert.ToInt32(Console.ReadLine());
  16.  
  17. Console.Write("Enter any number: ");
  18. exponent = Convert.ToInt32(Console.ReadLine());
  19.  
  20. power = 1;
  21. i = 1;
  22. //caculatinh power of given number
  23. while (i <= exponent)
  24. {
  25. power = power * basenumber;
  26. i++;
  27. }
  28. Console.Write("Power : "+ power);
  29.  
  30. Console.ReadLine();
  31. }
  32. }

Calculating power of a number using for loop


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. public class csharpExercise
  7. {
  8. static void Main(string[] args)
  9. {
  10. int basenumber, exponent, i, power;
  11.  
  12. // Reading number
  13. Console.Write("Enter any number: ");
  14. basenumber = Convert.ToInt32(Console.ReadLine());
  15.  
  16. Console.Write("Enter any number: ");
  17. exponent = Convert.ToInt32(Console.ReadLine());
  18.  
  19. power = 1;
  20. //caculatinh power of given number
  21. for (i = 1; i <= exponent; i++)
  22. power = power * basenumber;
  23.  
  24.  
  25. Console.Write("Power : "+ power);
  26.  
  27. Console.ReadLine();
  28. }
  29. }

Result

C# language techstudy

     





Related Topics




Popular Posts