Results 1 to 3 of 3
  1. #1
    sworfish is offline Member
    Join Date
    Oct 2014
    Posts
    1
    Rep Power
    0

    Default using for loop to calculate power (ex... x^4)

    i'm new to java and i'm trying to figure out how to calculate power in java using the for loop.

    the geometric sequence is (d*c^(n-1)).

    so it goes d, d*c^1, d*c^2,......., d*c^(n-1)


    ??????for (i=1; i<=n; i++){
    result
    }????????
    please help, i don't understand how to program this. how do i write this using for loop?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: using for loop to calculate power (ex... x^4)

    Suppose you know the value x^(n-1) and you are asked for the value x^n; take the previous value and multiply by x: x^(n-1)*x == x^n; as you should know, x^0 == 1. This is what it looks like in code:

    Java Code:
    double result= 1;
    for (int i= 0; i < n; i++)
       result*= x;
    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: using for loop to calculate power (ex... x^4)

    Is that c^(n-1), or (d*c)^(n-1)?

    Either way it doesn't really matter in terms of how to calculate the power using a loop, it just changes the start value.

    So how would you do it by hand, for a value 'x' and a value of 'n'? Forget about code for the minute.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. calculate making problem while using while() loop in ResultSet
    By raj.mscking@gmail.com in forum New To Java
    Replies: 6
    Last Post: 06-18-2012, 08:30 AM
  2. Replies: 23
    Last Post: 11-20-2011, 09:07 PM
  3. Replies: 8
    Last Post: 03-10-2011, 03:16 AM
  4. Power*MatchMaker 0.9.2
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-27-2007, 08:21 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •