Thread: power

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    54

    power

    how do you do powers with a for loop?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Just loop n times and multiply val by itself:
    Code:
    int power ( int val, int n )
    {
      int ret = 1;
    
      for ( int i = 0; i < n; i++ )
        ret *= val;
    
      return ret;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    30
    There is a function called exp();. This is found in math.h. The syntax is
    exp(number,power);

    I know this isn't a for loop, but it is a differrent method.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem With My Box
    By HaVoX in forum Tech Board
    Replies: 9
    Last Post: 10-15-2005, 07:38 AM
  2. No Power, but power being supplied.
    By jrahhali in forum Tech Board
    Replies: 6
    Last Post: 08-11-2005, 02:50 AM
  3. The destructive power of a nuclear bomb
    By InvariantLoop in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 03-24-2005, 02:46 AM
  4. Power supplies and demanding processors
    By Liger86 in forum Tech Board
    Replies: 12
    Last Post: 03-17-2005, 11:56 AM