In THREE separate languages, write functions to achieve the following WITHOUT using a built-in POWER function or operator. Input 1: A Number Input 2: A Power Output: The value of the Number raised to the Power in java
Answers
Answer:
dwuwiwowkwmsndvdcshsj wjwistsv enekekdkdjcuvvcy
Program in Java:
import java.util.*;
public class Main
{
static int power(int x, int y)
{
if(y != 0)
{
return (x * power(x, y-1));
}
else
{
return 1;
}
}
public static void main(String[] args)
{
Scanner Sc = new Scanner(System.in);
int x, y;
System.out.print("Enter base: ");
x = Sc.nextInt();
System.out.print("Enter power: ");
y = Sc.nextInt();
System.out.print(x + "^" + y + " = " + power(x, y));
}
}
Output:
Enter base: 2
Enter power: 5
2^5 = 32
Program in C++:
#include<iostream>
using namespace std;
int power(int x, int y)
{
int ans = 1;
for(int i = 1; i <= y; i++)
{
ans = ans * x;
}
return ans;
}
int main()
{
int x, y;
cout<<"Enter base: ";
cin>>x;
cout<<"Enter power: ";
cin>>y;
cout<<x<<"^"<<y<<" = "<<power(x, y);
return 0;
}
Output:
Enter base: 2
Enter power: 5
2^5 = 32
Program in Python:
def power(x, y):
ans = 1
for i in range(0, y):
ans = ans * x
return ans
x = int(input("Enter base : "))
y = int(input("Enter power : "))
print(x,"^",y," = ",power(x,y))
Output:
Enter base: 2
Enter power: 5
2 ^ 5 = 32