Results 1 to 3 of 3

Thread: Java math question

  1. #1
    Join Date
    Feb 2009
    Posts
    96

    Java math question

    i need to make a method in which a for loop is used to calculate 2 exponent n. Does anyone know the math formula for mutiplying a number by an inferred exponent?
    i have this
    public static void powerOf()
    {
    //declare variables
    int num;
    int pwr;
    System.out.println("The powers using a for statement are: ");
    //for statement
    for( num = 1; num <11; ++num){
    //squaring
    pwr =(2*num);
    System.out.println(" 2 to the power of "+ num + " is: " + pwr);

    }


    i need it so that when n is instantuated it is a power of 2 lets say soemthing liek 2 power 2 should be 4 and 2 power 3 is 8 etc..

  2. #2
    Join Date
    Aug 2009
    Posts
    7

    Re: Java math question

    I don't know about java but have a look at this C example using POW function:

    Code:
    /* pow example */
    #include <stdio.h>
    #include <math.h>
    
    int main ()
    {
      printf ("7 ^ 3 = %lf\n", pow (7,3));
      printf ("4.73 ^ 12 = %lf\n", pow (4.73,12));
      printf ("32.01 ^ 1.54 = %lf\n", pow (32.01,1.54));
      return 0;
    }
    for exponential function

    Code:
    /* exp example */
    #include <stdio.h>
    #include <math.h>
    
    int main ()
    {
      double param, result;
      param = 5.0;
      result = exp (param);
      printf ("The exponential value of %lf is %lf.\n", param, result );
      return 0;
    }

  3. #3
    Join Date
    Feb 2009
    Posts
    96

    Re: Java math question

    ty for the reply but i figured it out.

    public static void exponential()
    {
    //declare variables
    double num;
    double cons = 2;

    System.out.println("The powers using a for statement are: ");
    //for statement
    for( num = 1; num <11; ++num)
    {
    //powering

    System.out.println(" 2 to the power of "+ num + " is: " + Math.pow(cons, num));

    }

Similar Threads

  1. Basic Java question
    By colby02184 in forum Software Development
    Replies: 2
    Last Post: 11-11-2011, 05:08 PM
  2. Question to Hibernate in Java Development
    By Taarank in forum Software Development
    Replies: 5
    Last Post: 29-07-2010, 01:51 AM
  3. Java ME SDK 3.0 Math Class
    By Jona-thon in forum Software Development
    Replies: 5
    Last Post: 15-07-2010, 11:48 PM
  4. UML to Java question
    By imgr8 in forum Software Development
    Replies: 1
    Last Post: 20-04-2010, 01:05 PM
  5. Java - question on enum
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 02-03-2010, 10:50 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,174,206.22494 seconds with 16 queries