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


techstudy.org

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

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


Reverse a Number using while loop

  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int base, exponent,power, i;
  9.  
  10. //Reading base & exponent
  11. cout<<"Enter base: ";
  12. cin>>base;
  13. cout<<"Enter exponent: ";
  14. cin>>exponent;
  15.  
  16. power = 1;
  17. i = 1;
  18. //caculatinh power of given number
  19. while(i <= exponent)
  20. {
  21. power = power * base;
  22. i++;
  23. }
  24. cout<<"Power of "<<base<<" is: " <<power;
  25.  
  26. return 0;
  27. }

Reverse a Number using for loop

  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int base, exponent, i, power;
  9.  
  10. //Reading base & exponent
  11. cout<<"Enter base: ";
  12. cin>>base;
  13. cout<<"Enter exponent: ";
  14. cin>>exponent;
  15.  
  16. power = 1;
  17.  
  18. //caculatinh power of given number using for loop
  19. for(i=1; i<=exponent; i++)
  20. power = power * base;
  21. cout<<"Power of "<<base<<" is: " <<power;
  22.  
  23. return 0;
  24. }
  25.  

Result

C++ language techstudy.org

     





Related Topics




Popular Posts