Escriba un programa para calcular pow(x,n)

 

Dados dos enteros x y n, escribe una función para calcular x n . Podemos suponer que x y n son pequeños y que no se produce un desbordamiento.

program to calculate pow(x,n)

C++

// C++ program to calculate pow(x,n)
#include<iostream>
using namespace std;
class gfg
{
     
/* Function to calculate x raised to the power y */
public:
int power(int x, unsigned int y)
{
    if (y == 0)
        return 1;
    else if (y % 2 == 0)
        return power(x, y / 2) * power(x, y / 2);
    else
        return x * power(x, y / 2) * power(x, y / 2);
}
};
 
/* Driver code */
int main()
{
    gfg g;
    int x = 2;
    unsigned int y = 3;
 
    cout << g.power(x, y);
    return 0;
}
 
// This code is contributed by SoM15242

C

#include<stdio.h>
 
/* Function to calculate x raised to the power y */
int power(int x, unsigned int y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);
}
 
/* Program to test function power */
int main()
{
    int x = 2;
    unsigned int y = 3;
 
    printf("%d", power(x, y));
    return 0;
}

Java

class GFG {
    /* Function to calculate x raised to the power y */
    static int power(int x, int y)
    {
        if (y == 0)
            return 1;
        else if (y % 2 == 0)
            return power(x, y / 2) * power(x, y / 2);
        else
            return x * power(x, y / 2) * power(x, y / 2);
    }
 
    /* Program to test function power */
    public static void main(String[] args)
    {
        int x = 2;
        int y = 3;
 
        System.out.printf("%d", power(x, y));
    }
}
 
// This code is contributed by Smitha Dinesh Semwal

Python3

# Python3 program to calculate pow(x,n)
 
# Function to calculate x
# raised to the power y
def power(x, y):
 
    if (y == 0): return 1
    elif (int(y % 2) == 0):
        return (power(x, int(y / 2)) *
               power(x, int(y / 2)))
    else:
        return (x * power(x, int(y / 2)) *
                   power(x, int(y / 2)))
 
# Driver Code
x = 2; y = 3
print(power(x, y))
 
# This code is contributed by Smitha Dinesh Semwal.

C#

using System;
 
public class GFG {
     
    // Function to calculate x raised to the power y
    static int power(int x, int y)
    {
        if (y == 0)
            return 1;
        else if (y % 2 == 0)
            return power(x, y / 2) * power(x, y / 2);
        else
            return x * power(x, y / 2) * power(x, y / 2);
    }
 
    // Program to test function power
    public static void Main()
    {
        int x = 2;
        int y = 3;
 
        Console.Write(power(x, y));
    }
}
 
// This code is contributed by shiv_bhakt.

PHP

<?php
// Function to calculate x
// raised to the power y
function power($x, $y)
{
    if ($y == 0)
        return 1;
    else if ($y % 2 == 0)
        return power($x, (int)$y / 2) *
               power($x, (int)$y / 2);
    else
        return $x * power($x, (int)$y / 2) *
                    power($x, (int)$y / 2);
}
 
// Driver Code
$x = 2;
$y = 3;
 
echo power($x, $y);
     
// This code is contributed by ajit
?>

Javascript

<script>
// Javascript program to calculate pow(x,n)
 
// Function to calculate x
// raised to the power y
function power(x, y)
{
    if (y == 0)
        return 1;
    else if (y % 2 == 0)
        return power(x, parseInt(y / 2, 10)) *
               power(x, parseInt(y / 2, 10));
    else
        return x * power(x, parseInt(y / 2, 10)) *
                   power(x, parseInt(y / 2, 10));
}
 
// Driver code
let x = 2;
let y = 3;
 
document.write(power(x, y));
 
// This code is contributed by mukesh07
 
</script>

C++

/* Function to calculate x raised to the power y in O(logn)*/
int power(int x, unsigned int y)
{
    int temp;
    if( y == 0)
        return 1;
    temp = power(x, y / 2);
    if (y % 2 == 0)
        return temp * temp;
    else
        return x * temp * temp;
}
 
// This code is contributed by Shubhamsingh10

C

/* Function to calculate x raised to the power y in O(logn)*/
int power(int x, unsigned int y)
{
    int temp;
    if( y == 0)
        return 1;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}

Java

/* Function to calculate x raised to the power y in O(logn)*/
static int power(int x, int y)
{
    int temp;
    if( y == 0)
        return 1;
    temp = power(x, y / 2);
    if (y % 2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}
 
// This code is contributed by divyeshrabadiya07.

Python3

# Function to calculate x raised to the power y in O(logn)
def power(x,y):
    temp = 0
    if( y == 0):
        return 1
    temp = power(x, int(y / 2))
    if (y % 2 == 0)
        return temp * temp;
    else
        return x * temp * temp;
 
# This code is contributed by avanitrachhadiya2155

C#

/* Function to calculate x raised to the power y in O(logn)*/
static int power(int x, int y)
{
    int temp;
    if( y == 0)
        return 1;
    temp = power(x, y / 2);
    if (y % 2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}
 
// This code is contributed by divyesh072019.

Javascript

<script>
 
/* Function to calculate x raised to the power y in O(logn)*/
function power(x , y)
{
    var temp;
    if( y == 0)
        return 1;
    temp = power(x, y / 2);
    if (y % 2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}
 
// This code is contributed by todaysgaurav
 
</script>

C++

/* Extended version of power function
that can work for float x and negative y*/
#include <bits/stdc++.h>
using namespace std;
 
float power(float x, int y)
{
    float temp;
    if(y == 0)
        return 1;
    temp = power(x, y / 2);
    if (y % 2 == 0)
        return temp * temp;
    else
    {
        if(y > 0)
            return x * temp * temp;
        else
            return (temp * temp) / x;
    }
}
 
// Driver Code
int main()
{
    float x = 2;
    int y = -3;
    cout << power(x, y);
    return 0;
}
 
// This is code is contributed
// by rathbhupendra

C

/* Extended version of power function that can work
 for float x and negative y*/
#include<stdio.h>
 
float power(float x, int y)
{
    float temp;
    if( y == 0)
       return 1;
    temp = power(x, y/2);      
    if (y%2 == 0)
        return temp*temp;
    else
    {
        if(y > 0)
            return x*temp*temp;
        else
            return (temp*temp)/x;
    }
} 
 
/* Program to test function power */
int main()
{
    float x = 2;
    int y = -3;
    printf("%f", power(x, y));
    return 0;
}

Java

/* Java code for extended version of power function
that can work for float x and negative y */
class GFG {
     
    static float power(float x, int y)
    {
        float temp;
        if( y == 0)
            return 1;
        temp = power(x, y/2);
         
        if (y%2 == 0)
            return temp*temp;
        else
        {
            if(y > 0)
                return x * temp * temp;
            else
                return (temp * temp) / x;
        }
    }
     
    /* Program to test function power */
    public static void main(String[] args)
    {
        float x = 2;
        int y = -3;
        System.out.printf("%f", power(x, y));
    }
}
 
// This code is contributed by  Smitha Dinesh Semwal.

Python3

# Python3 code for extended version
# of power function that can work
# for float x and negative y
 
def power(x, y):
 
    if(y == 0): return 1
    temp = power(x, int(y / 2))
     
    if (y % 2 == 0):
        return temp * temp
    else:
        if(y > 0): return x * temp * temp
        else: return (temp * temp) / x
     
# Driver Code
x, y = 2, -3
print('%.6f' %(power(x, y)))
 
# This code is contributed by Smitha Dinesh Semwal.

C#

// C# code for extended version of power function
// that can work for float x and negative y
 
using System;
 
public class GFG{
     
    static float power(float x, int y)
    {
        float temp;
         
        if( y == 0)
            return 1;
        temp = power(x, y/2);
         
        if (y % 2 == 0)
            return temp * temp;
        else
        {
            if(y > 0)
                return x * temp * temp;
            else
                return (temp * temp) / x;
        }
    }
     
    // Program to test function power
    public static void Main()
    {
        float x = 2;
        int y = -3;
         
        Console.Write(power(x, y));
    }
}
 
// This code is contributed by shiv_bhakt.

PHP

<?php
// Extended version of power
// function that can work
// for float x and negative y
 
function power($x, $y)
{
    $temp;
    if( $y == 0)
    return 1;
    $temp = power($x, $y / 2);    
    if ($y % 2 == 0)
        return $temp * $temp;
    else
    {
        if($y > 0)
            return $x *
                   $temp * $temp;
        else
            return ($temp *
                    $temp) / $x;
    }
}
 
// Driver Code
$x = 2;
$y = -3;
echo power($x, $y);
 
// This code is contributed by ajit
?>

Javascript

<script>
 
// Javascript code for extended
// version of power function that
// can work for var x and negative y
function power(x, y)
{
    var temp;
     
    if (y == 0)
        return 1;
         
    temp = power(x, parseInt(y / 2));
 
    if (y % 2 == 0)
        return temp * temp;
    else
    {
        if (y > 0)
            return x * temp * temp;
        else
            return (temp * temp) / x;
    }
}
 
// Driver code
var x = 2;
var y = -3;
 
document.write( power(x, y).toFixed(6));
 
// This code is contributed by aashish1995
 
</script>

C++

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
int power(int x, int y)
{
    // If x^0 return 1
    if (y == 0)
        return 1;
    // If we need to find of 0^y
    if (x == 0)
        return 0;
    // For all other cases
    return x * power(x, y - 1);
}
 
// Driver Code
int main()
{
    int x = 2;
    int y = 3;
    cout << (power(x, y));
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

C

// C program for the above approach
#include <stdio.h>
 
int power(int x, int y)
{
    // If x^0 return 1
    if (y == 0)
        return 1;
    // If we need to find of 0^y
    if (x == 0)
        return 0;
    // For all other cases
    return x * power(x, y - 1);
}
 
// Driver Code
int main()
{
    int x = 2;
    int y = 3;
    printf("%d\n",power(x, y));
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Java

// Java program for the above approach
import java.io.*;
 
class GFG {
    public static int power(int x, int y)
    {
         
        // If x^0 return 1
        if (y == 0)
            return 1;
         
        // If we need to find of 0^y
        if (x == 0)
            return 0;
         
        // For all other cases
        return x * power(x, y - 1);
    }
   
    // Driver Code
    public static void main(String[] args)
    {
        int x = 2;
        int y = 3;
 
        System.out.println(power(x, y));
    }
}

Python3

# Python3 program for the above approach
def power(x, y):
     
    # If x^0 return 1
    if (y == 0):
        return 1
     
    # If we need to find of 0^y
    if (x == 0):
        return 0
     
    # For all other cases
    return x * power(x, y - 1)
  
# Driver Code
x = 2
y = 3
 
print(power(x, y))
 
# This code is contributed by shivani.

C#

// C# program for the above approach
using System;
 
class GFG{
     
public static int power(int x, int y)
{
     
    // If x^0 return 1
    if (y == 0)
        return 1;
     
    // If we need to find of 0^y
    if (x == 0)
        return 0;
     
    // For all other cases
    return x * power(x, y - 1);
}
 
// Driver Code
public static void Main(String[] args)
{
    int x = 2;
    int y = 3;
 
    Console.WriteLine(power(x, y));
}
}
 
// This code is contributed by Rajput-Ji

Javascript

<script>
 
// javascript program for the above approach
    function power(x , y) {
 
        // If x^0 return 1
        if (y == 0)
            return 1;
 
        // If we need to find of 0^y
        if (x == 0)
            return 0;
 
        // For all other cases
        return x * power(x, y - 1);
    }
 
    // Driver Code
     
        var x = 2;
        var y = 3;
 
        document.write(power(x, y));
 
// This code is contributed by Rajput-Ji
 
</script>

C++

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
int power(int x, int y)
{
     
    // return type of pow()
    // function is double
    return (int)pow(x, y);
}
 
// Driver Code
int main()
{
    int x = 2;
    int y = 3;
 
    cout << (power(x, y));
}
 
// This code is contributed by hemantraj712.

Java

// Java program for the above approach
import java.io.*;
 
class GFG {
    public static int power(int x, int y)
    {
         
        // Math.pow() is a function that
        // return floating number
        return (int)Math.pow(x, y);
    }
   
    // Driver Code
    public static void main(String[] args)
    {
        int x = 2;
        int y = 3;
 
        System.out.println(power(x, y));
    }
}

Python3

# Python3 program for the above approach
def power(x, y):
     
    # Return type of pow()
    # function is double
    return pow(x, y)
     
# Driver Code
x = 2
y = 3
 
print(power(x, y))
 
# This code is contributed by susmitakundugoaldanga

C#

// C# program for the above approach
 
using System;
 
public class GFG {
 
    public static int power(int x, int y)
    {
          
        // Math.pow() is a function that
        // return floating number
        return (int)Math.Pow(x, y);
    }
 
    // Driver code
    static public void Main()
    {
        int x = 2;
        int y = 3;
  
        Console.WriteLine(power(x, y));
    }
}

Javascript

<script>
 
// Javascript program for the above approach
 
    function power( x, y)
    {
         
        // Math.pow() is a function that
        // return floating number
        return parseInt(Math.pow(x, y));
    }
 
    // Driver Code
     
        let x = 2;
        let y = 3;
 
        document.write(power(x, y));
         
// This code is contributed by sravan kumar
 
</script>

C++

// C++ program for the above approach
#include <iostream>
using namespace std;
 
int power(int x, int y)
{
    int result = 1;
    while (y > 0) {
      if(y&1==1) // y is odd
      {
        result=result*x;
      }
      x=x*x;
      y=y>>1; // y=y/2;
    }
    return result;
}
 
// Driver Code
int main()
{
    int x = 2;
    int y = 3;
 
    cout << (power(2, 3));
    return 0;
}
 
// This code is contributed bySuruchi Kumari

Java

// Java program for above approach
class GFG{
 
static int power(int x, int y)
{
    int result = 1;
    while (y > 0) {
      if(y%2!=0) // y is odd
      {
        result=result*x;
      }
      x=x*x;
      y=y>>1; // y=y/2;
    }
    return result;
}
 
// Driver Code
public static void main(String[] args)
{
    int x = 2;
    int y = 3;
     
    System.out.println(power(x, y));
}
}
 
// This code is contributed by Suruchi Kumari

Python3

# Python program for the above approach
def power( x, y):
 
    result = 1
    while (y > 0):
        if (y % 2 == 0):
        # y is even
         
            x = x * x
            y = y / 2
         
        else:
        # y isn't even
         
            result = result * x
            y = y - 1
         
     
    return result
 
 
# Driver Code
x = 2
y = 3
print((power(x, y)))
 
# This code is contributed by shivanisinghss2110

C#

// C# program for above approach
using System;
 
class GFG{
 
static int power(int x, int y)
{
    int result = 1;
     
    while (y > 0)
    {
         
        // y is even
        if (y % 2 == 0)
        {
            x = x * x;
            y = y / 2;
        }
         
        // y isn't even
        else
        {
            result = result * x;
            y = y - 1;
        }
    }
    return result;
}
 
// Driver Code
public static void Main(String[] args)
{
    int x = 2;
    int y = 3;
     
    Console.Write(power(x, y));
}
}
 
// This code is contributed by shivanisinghss2110

Javascript

<script>
// Javascript program for the above approach
 
function power(x,y)
{
    let result = 1;
    while (y > 0) {
        if (y % 2 == 0) // y is even
        {
            x = x * x;
            y = Math.floor(y / 2);
        }
        else // y isn't even
        {
            result = result * x;
            y = y - 1;
        }
    }
    return result;
}
 
// Driver Code
let x = 2;
let y = 3;
document.write(power(x, y))
 
// This code is contributed by rag2127
</script>

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *