Sunday, March 11, 2018

Find power of a number

Find power of a number
Send Feedback

Write a program to find x to the power n (i.e. x^n). Take x and n from the user. You need to print the answer.

Input format :
Two integers x and n (separated by space)
Output Format :
x^n (i.e. x raise to the power n)
Sample Input 1 :
 3 4
Sample Output 1 :
81
Sample Input 2 :
 2 5
Sample Output 2 :
32

import java.util.Scanner;

public class Main
{

public static void main(String[] args)
{
// TODO Auto-generated method stub
int pro=1;

//System.out.print("Enter the base number:");
Scanner s = new Scanner(System.in);
int b = s.nextInt();
// ask for exponent
//System.out.print("Enter the exponent:");
int e = s.nextInt();
//String exponent = s.nextLine();

for (int i=1;i<=e;i++)
{
pro = pro * b;

}
//System.out.println("the power of given number is"+pro);
System.out.println(pro);



}

}

No comments:

Post a Comment