• Forum
  • Lounge
  • Java . I am trying to get the power of a

 
Java . I am trying to get the power of a number. This is what i have.

package Richcalculatorlab;

import java.text.DecimalFormat;
import java.util.Scanner;

public class Richcalculatorlab
{



public static void main(String[] args)
{

double fp1, fp2;
String operation;

Scanner scan = new Scanner( System.in);

DecimalFormat twoDecimals = new DecimalFormat( " #0.0");

//print a welcome message
System.out.println( "Welcome to the Calculator");


//read the operands
System.out.println( "Enter the first operand: ");
fp1 = scan.nextDouble();
System.out.print("Enter the second operand: ");
fp2 = scan.nextDouble();

//print a menu, then prompt for the operation
System.out.println( "\nOperations are: "
+"\n\t 1 for + or addition"
+"\n\t 2 for - for subtraction"
+"\n\t 3 for * for mulplication"
+"\n\t 4 for / for division"
+"\n\t 5 for ^ for power");
System.out.print( "Enter your selection: ");
operation= scan.next( );
operation= operation.toUpperCase( );

//perform the operation and print the result
switch ( operation)
{
case "1":
case "+":
System.out.println("The sum is "
+ twoDecimals.format( fp1+ fp2 ) );
break;
case "2":
case "-":
System.out.println("The diffrence is "
+ twoDecimals.format( fp1 - fp2) );
break;
case "3":
case "*":
System.out.println ("The product is"
+twoDecimals.format(fp1 * fp2) );
break;
case "4":
case "/":
if ( fp2== 0.0 )
System.out.println( "Dividing by 0 is not allowed, please enter a non zero value for fp2");
else
System.out.println( "The quotient is "
+ twoDecimals.format( fp1 / fp2 ) );
break;
case "5":
case "pow":
double result = Math.pow( fp1, fp2 );
System.out.println ("The power is"
+twoDecimals.format(fp2*2) );

default:
System.out.println( operation + " is not valid.");
}
}
}
When I hit 5 I don't get the power of. It is a calculator. addition ,subtraction, mulplication and division work. Thank you
Change this:

1
2
System.out.println ("The power is"
+twoDecimals.format(fp2*2) );


to

1
2
System.out.println ("The power is"
+twoDecimals.format(Math.pow( fp1, fp2 ) );
Topic archived. No new replies allowed.