Program to make a function that print (n^x) the result of any power of any number in java.
import java.util.*;
public class function {
public static int power(int x,int n){
int res=1;
for(int i=1; i<=n; i++){
res=res*x;
}
return res;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter the value of x=");
int x=sc.nextInt();
System.out.println("enter the value of n=");
int n=sc.nextInt();
System.out.print(" the result is="+power(x,n));
}
}
■》Output->
enter the value of x= 2
enter the value of n= 5
The result is= 32
Comments
Post a Comment