Thread: Raising the power of an interger

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    56

    Raising the power of an interger

    Hello forum!
    I'm absolutely befuddled on my current enigma.
    I have an assignment that is due for my class. The question is - "Display the value of 2 raised to the power of the value input in (1), i.e. display 2n, where n is the value from (1)"
    I know it's simple, but i am absolutely terrible at math. I know it involves the for loop, but i don't know how i would utilize it for that.
    Any help is appreciated!

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    I forgot to mention that where it says "(1)", is basically my first problem for my program to accept an interger.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    i am befuddled on how someone will be in front of a computer, and cant google, or use a search box...

    what is the power command in c..

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    I forgot to specify that i can only use a loop, such a "for".
    I have googled it, but as i said, i'm still confused. Which is why i came to this forum.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    (1) What is 23?
    (2) What does 23 look like written out in multiplication?

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Crossfire View Post
    i am befuddled on how someone will be in front of a computer, and cant google, or use a search box...

    what is the power command in c..
    I'm assuming that the OP isn't supposed to be using library functions for this, as it's more educational to write their own code to perform this calculation. But I could be wrong.

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    "I'm assuming that the OP isn't supposed to be using library functions for this, as it's more educational to write their own code to perform this calculation. But I could be wrong."
    Yes! Exactly!
    I need a program to except an interger, and then use that number as an exponent for 2. I have to do this though using the for loop.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by sourpatchkid View Post
    "I'm assuming that the OP isn't supposed to be using library functions for this, as it's more educational to write their own code to perform this calculation. But I could be wrong."
    Yes! Exactly!
    I need a program to except an interger, and then use that number as an exponent for 2. I have to do this though using the for loop.
    Can you answer my questions in post #5?

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    I understand exponents, i need to have my program use a "for" loop, to solve a user inputted exponent.
    I thought you guys knew how to program?

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I thought you guys knew how to program?
    Indeed. And we offer our help here to assist others in learning. Therefore, you are more likely to get hints that will enable you to solve the problem yourself, than a spoon-fed answer.

    Here's the sites homework policy, fyi.

    Can you write a "for()" loop? Let's start there.

  12. #12
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    "Can you answer my questions in post #5?"
    2*2*2
    -_-

  13. #13
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by sourpatchkid View Post
    I am absolutely terrible at math.
    Perhaps a few math examples will help:

    2^0 = 1
    2^1 = 1 · 2
    2^2 = 1 · 2 · 2
    2^3 = 1 · 2 · 2 · 2

    So this is the math part, can you try to duplicate this math using a for loop?

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    So a "for()" loop has three parts: initialization, condition, and the iterative change.

    Initialization might typically start at zero, if you're using a counter.

    Since an exponent is a number (x) multiplied by itself a certain amount of times (n), the body of the loop should keep a running product of x.

    Increment the counter with the iterative change (the correct terminology escapes me ATM) and keep looping this process. How many times (until the condition is met)? n times.

  15. #15
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    "Indeed. And we offer our help here to assist others in learning. Therefore, you are more likely to get hints that will enable you to solve the problem yourself, than a spoon-fed answer.

    Here's the sites homework policy, fyi.

    Can you write a "for()" loop? Let's start there."

    Indeed i can.

    Code:
    For(loop_counter = 0, loop_counter < 10, loop_counter++)
    {
    printf("loop %d", loop_counter);
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Raising an Integer to a power
    By aileentas in forum C Programming
    Replies: 14
    Last Post: 09-19-2012, 03:58 AM
  2. Raising to a power
    By teelnaw in forum C Programming
    Replies: 6
    Last Post: 11-07-2010, 01:35 AM
  3. Help with checking, raising to the power of 2
    By philippe in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 07:30 PM
  4. raising to a power
    By next_to_nothing in forum C++ Programming
    Replies: 5
    Last Post: 10-22-2002, 04:00 AM
  5. Raising to a power
    By Nate2430 in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2001, 04:44 PM