473,400 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

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

3
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.

Expand|Select|Wrap|Line Numbers
  1.  import javax.swing.JOptionPane; 
  2.  
  3. class PowerOf {
  4.     public static void main(String[] args){
  5.  
  6.         String strX;
  7.         double x;
  8.         String strN;
  9.         int n;
  10.         int counter=1;
  11.         double value;
  12.  
  13.         strX = JOptionPane.showInputDialog(null, "Enter the x value: ");
  14.         x = Double.parseDouble(strX);
  15.  
  16.         strN = JOptionPane.showInputDialog(null, "Enter the power(n) value: ");
  17.         n = Integer.parseInt(strN);
  18.  
  19.         for (counter=1; counter<=10; counter++)//to print the value
  20.         {
  21.             pow(x,n);
  22.             System.out.println(pow(x,n));
  23.             counter++;
  24.         }    
  25.  
  26.         while (counter!=10)//for checking the answer
  27.         {
  28.             counter++;
  29.  
  30.  
  31.             value=Math.pow(x,n);
  32.             System.out.println(value);    
  33.             n++;
  34.         }
  35.     }
  36.  
  37.     public static double pow(double x, int n){
  38.  
  39.         double value=1;
  40.  
  41.         for (int counter=1,counter<n, counter++)
  42.         {
  43.             value=1/value*2;
  44.  
  45.         }
  46.  
  47.             value=value*2;
  48.  
  49.         n++;
  50.         return value;
  51.     }
  52.  
  53. }
  54.  
  55.  
Hope u can spare the time to look through. Would appreciate it. Thanks
Mar 10 '07 #1
1 5026
r035198x
13,262 8TB
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.

Expand|Select|Wrap|Line Numbers
  1.  import javax.swing.JOptionPane; 
  2.  
  3. class PowerOf {
  4.     public static void main(String[] args){
  5.  
  6.         String strX;
  7.         double x;
  8.         String strN;
  9.         int n;
  10.         int counter=1;
  11.         double value;
  12.  
  13.         strX = JOptionPane.showInputDialog(null, "Enter the x value: ");
  14.         x = Double.parseDouble(strX);
  15.  
  16.         strN = JOptionPane.showInputDialog(null, "Enter the power(n) value: ");
  17.         n = Integer.parseInt(strN);
  18.  
  19.         for (counter=1; counter<=10; counter++)//to print the value
  20.         {
  21.             pow(x,n);
  22.             System.out.println(pow(x,n));
  23.             counter++;
  24.         }    
  25.  
  26.         while (counter!=10)//for checking the answer
  27.         {
  28.             counter++;
  29.  
  30.  
  31.             value=Math.pow(x,n);
  32.             System.out.println(value);    
  33.             n++;
  34.         }
  35.     }
  36.  
  37.     public static double pow(double x, int n){
  38.  
  39.         double value=1;
  40.  
  41.         for (int counter=1,counter<n, counter++)
  42.         {
  43.             value=1/value*2;
  44.  
  45.         }
  46.  
  47.             value=value*2;
  48.  
  49.         n++;
  50.         return value;
  51.     }
  52.  
  53. }
  54.  
  55.  
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

Expand|Select|Wrap|Line Numbers
  1.  import javax.swing.JOptionPane; 
  2. class PowerOf {
  3.  public static void main(String[] args){
  4.   String strX;
  5.   double x;
  6.   String strN;
  7.   int n;
  8.   int counter=1;
  9.   double value;
  10.   strX = JOptionPane.showInputDialog(null, "Enter the x value: ");
  11.   x = Double.parseDouble(strX);
  12.   strN = JOptionPane.showInputDialog(null, "Enter the power(n) value: ");
  13.   n = Integer.parseInt(strN);
  14.   double check = Math.pow(x, n);
  15.   value = PowerOf.pow(x, n);
  16.   System.out.print("Correct value is "+check+" : My value is "+value);
  17.  }
  18.  public static double pow(double x, int n){
  19.   double value = 0.0;
  20.   if(n < 0) {
  21.    //write logic here
  22.   }
  23.   else if(n == 0) {
  24.    //write logic here
  25.   }
  26.   else {
  27.    for (int i = 1; i < n; i++) {
  28.    //write logic here
  29.    }
  30.   }
  31.   return value;
  32.  }
  33. }
  34.  
  35.  
Mar 10 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: da Vinci | last post by:
Hi Gents, This is what I am trying to do. Say you have a double or a float with the value 14.5624 for example. How could I take that variable and get the 14 into an integer variable and the...
3
by: Young J. Putt | last post by:
I have a vb.net datagrid bound to a dataview on a windows form. I want to use the datagrid to display and filter a list of items, but since the data is complex, I don't want the user to edit the...
1
by: Joe | last post by:
Is there a way to get 1000000 to display as 1,000k using double.ToString()? -Joe
2
by: matrim | last post by:
What I'm trying to do: 1. Attempt to open the file. The filename, c:\\windData.txt, should be hardcoded into your program. Note the two slash characters in the file name. If the file cannot be...
8
by: Chris Stankevitz | last post by:
Q1: Does c++ provide pow(int,int)? Q2: If not, why not? Thanks, Chris
9
by: Sarath | last post by:
Why the following happening? UINT m_uArrXpos; UINT **p = m_uArrXposSchema; // Failed to compile UINT **p = (UINT**)m_uArrXposSchema; // Success compilation
1
by: Maarten van der Cammen | last post by:
Hi, In a table I have two numeric fields (e.g. FieldA and FieldB). Both fields have double precicion (field size "double"), Format "Fixed" and Decimal places "3" since I want to calculate with...
4
by: sharptool | last post by:
Please, please, please make suggestions on how to resolve these errors. I am trying to build a program that computes a, b, c for the quadratic equation but I'm not having any luck. It's been 5...
1
by: jombloboy | last post by:
I'm trying to find the biggest fibonacci number a double variable can handle, my loop looks like this: double num1 = 1; double num2 = 2; double fib = 0; { while ( fib >= 0 ) ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.