Skip to content

techtrendings.com

Let's explore

Menu
Menu

Program to find power of a number

Posted on November 11, 2022November 11, 2022 by Avidlearner

In computer programming, power is denoted by symbol ^ . To calculate the power y of a number x, we use x^y. For example

Let’s say x = 2, y = 5

Output of x^y = 32.

Below is the implementation of the same

#include<iostream>
using namespace std;
int pow(int x, int y) {
   int i,pow_var=1;
   if(y == 0)
   return 1;
   for(i=1;i<=y;i++)
   pow_var=pow_var*x;
   return pow_var;
}
int main() {
   int x = 2;
   int y = 5;
   cout<<"Number: "<<x<<endl;
   cout<<"Power:"<<y<<endl;
   cout<<"x^y = "<<pow(x,y);
   return 0;
}

Output:
Number:2
Power:5
x^y = 32

In the above program, the function pow() is used to calculate the power of a number x. IIn the pow function, we have use a for loop is used which runs from 1 to power variable(y). For each iteration of the loop, x is multiplied with pow_var to keep multiplying itself till power value.

So, x is multiplied with itself y times and the result is stored in pow_value. This leads to x^y being stored in pow_var and then returned to the main() function.

Related

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Implement Trie Data Structure in C++- LeetCode
  • How TLS Works
  • C++ – Factory Design Pattern – Creation Design Pattern
  • C++ – Strategy Design Pattern – Behavioral Design Pattern
  • LFU Cache Implementation – LeetCode

Recent Comments

  • automatically like friends photos on instagram on Program to find unpaired element in an Array in C++|Leetcode |techtrendings
  • Twicsy on Program to find unpaired element in an Array in C++|Leetcode |techtrendings

Archives

  • January 2023
  • November 2022
  • August 2022
  • June 2022
  • May 2022
  • March 2022
  • February 2022
  • January 2022

Categories

  • Algorithm
  • Algorithm
  • C++
  • Design Patterns
  • Multithreading
  • OS Concepts
  • Programming
  • Uncategorized

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Join Our Mailing List for the Latest News and Updates.

© 2023 techtrendings.com | Powered by Superbs Personal Blog theme