Free CoreJava Programs Source codes | Examples | Video Tutorials Download

Wednesday, January 21, 2009

Power Function

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

/**

* Write a program which accepts a base value and a power value and calculates

* base raised to power write a program which accepts base and power and prints

* ouput in the following format eg. 2 raised to 1 is 2 2 raised to 2 is 4 2

* raised to 3 is 8

*

*

*/

public class BasePower {

/**

* calculates the power values, ask for two inputs. eg. 2 raised to 1 is 2

*/

public static void calcPower() {

try {

// get input stream of console

BufferedReader brConsole = new BufferedReader(

new InputStreamReader(System.in));

// get base no input

System.out.println("Enter Base Value : ");

String strInput = brConsole.readLine();

int intBase = Integer.parseInt(strInput);

// get power value input

System.out.println("Enter Power Value : ");

strInput = brConsole.readLine();

int intPower = Integer.parseInt(strInput);

// calculate power

int result = (int) Math.pow(intBase, intPower);

// display result

System.out.println(intBase + " raised to " + intPower + " is "

+ result);

} catch (Exception e) {

System.out.println("Error : Arithmetic Exception Generated!!");

e.printStackTrace();

}

}

/**

* execution begin from here

*

* @param args

* commandline arguments

*/

public static void main(String[] args) {

calcPower();

}

}

No comments:

Post a Comment

Followers