Write you own Power without using multiplication(*) and division(/) operators
Method 1 (Using Nested Loops)
We can calculate power by using repeated addition.
For example to calculate 5^6.
1) First 5 times add 5, we get 25. (5^2)
2) Then 5 times add 25, we get 125. (5^3)
3) Then 5 time add 125, we get 625 (5^4)
4) Then 5 times add 625, we get 3125 (5^5)
5) Then 5 times add 3125, we get 15625 (5^6)
C
#include<stdio.h> /* Works only if a >= 0 and b >= 0 */ int pow ( int a, int b) { if (b == 0) return 1; int answer = a; int increment = a; int i, j; for (i = 1; i < b; i++) { for (j = 1; j < a; j++) { answer += increment; } increment = answer; } return answer; } /* driver program to test above function */ int main() { printf ( "\n %d" , pow (5, 3)); getchar (); return 0; } |
chevron_right
filter_none
Java
import java.io.*; class GFG { /* Works only if a >= 0 and b >= 0 */ static int pow( int a, int b) { if (b == 0 ) return 1 ; int answer = a; int increment = a; int i, j; for (i = 1 ; i < b; i++) { for (j = 1 ; j < a; j++) { answer += increment; } increment = answer; } return answer; } // driver program to test above function public static void main(String[] args) { System.out.println(pow( 5 , 3 )); } } // This code is contributed by vt_m. |
chevron_right
filter_none
Python
# Python 3 code for power # function # Works only if a >= 0 and b >= 0 def pow (a,b): if (b = = 0 ): return 1 answer = a increment = a for i in range ( 1 ,b): for j in range ( 1 ,a): answer + = increment increment = answer return answer # drive code print ( pow ( 5 , 3 )) # this code is contributed # by Sam007 |
chevron_right
filter_none
C#
using System; class GFG { /* Works only if a >= 0 and b >= 0 */ static int pow( int a, int b) { if (b == 0) return 1; int answer = a; int increment = a; int i, j; for (i = 1; i < b; i++) { for (j = 1; j < a; j++) { answer += increment; } increment = answer; } return answer; } // driver program to test // above function public static void Main() { Console.Write(pow(5, 3)); } } // This code is contributed by Sam007 |
chevron_right
filter_none
PHP
<?php // Works only if a >= 0 // and b >= 0 function poww( $a , $b ) { if ( $b == 0) return 1; $answer = $a ; $increment = $a ; $i ; $j ; for ( $i = 1; $i < $b ; $i ++) { for ( $j = 1; $j < $a ; $j ++) { $answer += $increment ; } $increment = $answer ; } return $answer ; } // Driver Code echo ( poww(5, 3)); // This code is contributed by nitin mittal. ?> |
chevron_right
filter_none
Ouput :
125
Method 2 (Using Recursion)
Recursively add a to get the multiplication of two numbers. And recursively multiply to get a raise to the power b.
C
#include<stdio.h> /* A recursive function to get a^b Works only if a >= 0 and b >= 0 */ int pow ( int a, int b) { if (b) return multiply(a, pow (a, b-1)); else return 1; } /* A recursive function to get x*y */ int multiply( int x, int y) { if (y) return (x + multiply(x, y-1)); else return 0; } /* driver program to test above functions */ int main() { printf ( "\n %d" , pow (5, 3)); getchar (); return 0; } |
chevron_right
filter_none
Java
import java.io.*; class GFG { /* A recursive function to get a^b Works only if a >= 0 and b >= 0 */ static int pow( int a, int b) { if (b > 0 ) return multiply(a, pow(a, b - 1 )); else return 1 ; } /* A recursive function to get x*y */ static int multiply( int x, int y) { if (y > 0 ) return (x + multiply(x, y - 1 )); else return 0 ; } /* driver program to test above functions */ public static void main(String[] args) { System.out.println(pow( 5 , 3 )); } } // This code is contributed by vt_m. |
chevron_right
filter_none
Python
def pow (a,b): if (b): return multiply(a, pow (a, b - 1 )); else : return 1 ; # A recursive function to get x*y * def multiply(x, y): if (y): return (x + multiply(x, y - 1 )); else : return 0 ; # driver program to test above functions * print ( pow ( 5 , 3 )); # This code is contributed # by Sam007 |
chevron_right
filter_none
C#
using System; class GFG { /* A recursive function to get a^b Works only if a >= 0 and b >= 0 */ static int pow( int a, int b) { if (b > 0) return multiply(a, pow(a, b - 1)); else return 1; } /* A recursive function to get x*y */ static int multiply( int x, int y) { if (y > 0) return (x + multiply(x, y - 1)); else return 0; } /* driver program to test above functions */ public static void Main() { Console.Write(pow(5, 3)); } } // This code is contributed by Sam007 |
chevron_right
filter_none
PHP
<?php /* A recursive function to get a^b Works only if a >= 0 and b >= 0 */ function p_ow( $a , $b ) { if ( $b ) return multiply( $a , p_ow( $a , $b - 1)); else return 1; } /* A recursive function to get x*y */ function multiply( $x , $y ) { if ( $y ) return ( $x + multiply( $x , $y - 1)); else return 0; } // Driver Code echo pow(5, 3); // This code is contributed by anuj_67. ?> |
chevron_right
filter_none
Ouput :
125
Please write comments if you find any bug in above code/algorithm, or find other ways to solve the same problem.
Recommended Posts:
- Multiply two integers without using multiplication, division and bitwise operators, and no loops
- Divide two integers without using multiplication, division and mod operator | Set2
- Compute power of power k times % m
- Check if given number is a power of d where d is a power of 2
- Find power of power under mod of a prime
- DFA based division
- Modular Division
- Division without using '/' operator
- Trick for modular division ( (x1 * x2 .... xn) / b ) mod (m)
- Find the number after successive division
- Program to compute division upto n decimal places
- Larger of a^b or b^a (a raised to power b or b raised to power a)
- Booth’s Multiplication Algorithm
- Multiplication of two complex numbers given as strings
- Check for integer overflow on multiplication