LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Using powers based on user input (http://www.linuxquestions.org/questions/programming-9/using-powers-based-on-user-input-4175439074/)

curious95 11-28-2012 02:14 AM

Using powers based on user input
 
i noticed that C does not have an exponentiation operator. So i was wondering how does one add a power to variable which is based on user input? example:
Code:

int x;
scanf("%d", &x);
printf("%d",xe2 + xe5);

where e is an exponentiation character, on a calculator e would be ^. (so x^2 + x^5)

JohnGraham 11-28-2012 03:07 AM

There's the function pow() (and associated powf() and powl()) in math.h.

acid_kewpie 11-28-2012 03:07 AM

ok, so use "e" is pretty chuffing confusing given e referes to the exponentiation function / inverse natural log. ^ would mean power normally, not exponent, so 2^5 is 25, not 200000, which is 2 x 10^5.

Use the math.h library, with the pow() function (which also contains exp() for the real e() function)

curious95 11-28-2012 06:41 AM

understood, thanks for the answers :)


All times are GMT -5. The time now is 07:12 AM.