Help with coding using pow(double x,int n)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • R69D
    New Member
    • Mar 2007
    • 3

    Help with coding using pow(double x,int n)

    Write and test the following method that implements the power function:

    static double pow(double x, int n)

    This method returns the value of x raised to the power n. For example pow(2.0, -3) would return

    2-3 = 0.125

    For each value of pow(x,n) that you print, also print the value of Math.pow(x,n) to check your results.

    Here is a sample run:
    0.125
    0.25
    0.5
    1.0
    2.0
    4.0
    8.0
    16.0
    32.0
    64.0
    Press any key to continue.....

    So far,I have done the coding up to this far..I can't seem to get it right.

    Code:
     import javax.swing.JOptionPane; 
     
    class PowerOf {
    	public static void main(String[] args){
     
    		String strX;
    		double x;
    		String strN;
    		int n;
    		int counter=1;
    		double value;
     
    		strX = JOptionPane.showInputDialog(null, "Enter the x value: ");
    		x = Double.parseDouble(strX);
     
    		strN = JOptionPane.showInputDialog(null, "Enter the power(n) value: ");
    		n = Integer.parseInt(strN);
     
    		for (counter=1; counter<=10; counter++)//to print the value
    		{
    			pow(x,n);
    			System.out.println(pow(x,n));
    			counter++;
    		}	
     
    		while (counter!=10)//for checking the answer
    		{
    			counter++;
     
     
    			value=Math.pow(x,n);
    			System.out.println(value);	
    			n++;
    		}
    	}
     
    	public static double pow(double x, int n){
     
    		double value=1;
     
    		for (int counter=1,counter<n, counter++)
    		{
    			value=1/value*2;
     
    		}
     
    			value=value*2;
     
    		n++;
    		return value;
    	}
     
    }
    Hope u can spare the time to look through. Would appreciate it. Thanks
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by R69D
    Write and test the following method that implements the power function:

    static double pow(double x, int n)

    This method returns the value of x raised to the power n. For example pow(2.0, -3) would return

    2-3 = 0.125

    For each value of pow(x,n) that you print, also print the value of Math.pow(x,n) to check your results.

    Here is a sample run:
    0.125
    0.25
    0.5
    1.0
    2.0
    4.0
    8.0
    16.0
    32.0
    64.0
    Press any key to continue.....

    So far,I have done the coding up to this far..I can't seem to get it right.

    Code:
     import javax.swing.JOptionPane; 
     
    class PowerOf {
    	public static void main(String[] args){
     
    		String strX;
    		double x;
    		String strN;
    		int n;
    		int counter=1;
    		double value;
     
    		strX = JOptionPane.showInputDialog(null, "Enter the x value: ");
    		x = Double.parseDouble(strX);
     
    		strN = JOptionPane.showInputDialog(null, "Enter the power(n) value: ");
    		n = Integer.parseInt(strN);
     
    		for (counter=1; counter<=10; counter++)//to print the value
    		{
    			pow(x,n);
    			System.out.println(pow(x,n));
    			counter++;
    		}	
     
    		while (counter!=10)//for checking the answer
    		{
    			counter++;
     
     
    			value=Math.pow(x,n);
    			System.out.println(value);	
    			n++;
    		}
    	}
     
    	public static double pow(double x, int n){
     
    		double value=1;
     
    		for (int counter=1,counter<n, counter++)
    		{
    			value=1/value*2;
     
    		}
     
    			value=value*2;
     
    		n++;
    		return value;
    	}
     
    }
    Hope u can spare the time to look through. Would appreciate it. Thanks
    When postind code please use code tags.

    You need to handle all the possible scenarios using if-else
    I have modified your program a bit. You should be able to finish it up now

    Code:
     import javax.swing.JOptionPane; 
    class PowerOf {
     public static void main(String[] args){
      String strX;
      double x;
      String strN;
      int n;
      int counter=1;
      double value;
      strX = JOptionPane.showInputDialog(null, "Enter the x value: ");
      x = Double.parseDouble(strX);
      strN = JOptionPane.showInputDialog(null, "Enter the power(n) value: ");
      n = Integer.parseInt(strN);
      double check = Math.pow(x, n);
      value = PowerOf.pow(x, n);
      System.out.print("Correct value is "+check+" : My value is "+value);
     }
     public static double pow(double x, int n){
      double value = 0.0;
      if(n < 0) {
       //write logic here
      }
      else if(n == 0) {
       //write logic here
      }
      else {
       for (int i = 1; i < n; i++) {
       //write logic here
       }
      }
      return value;
     }
    }

    Comment

    Working...