Register a domain and help support LQ
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org [SOLVED] Using powers based on user input
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 11-28-2012, 01:14 AM #1 curious95 Member   Registered: Oct 2012 Location: /home/v Distribution: Slackware 14.0 Posts: 83 Rep: 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)
 11-28-2012, 02:07 AM #2 JohnGraham Member   Registered: Oct 2009 Posts: 467 Rep: There's the function pow() (and associated powf() and powl()) in math.h.
 11-28-2012, 02:07 AM #3 acid_kewpie Moderator   Registered: Jun 2001 Location: UK Distribution: Gentoo, RHEL, Fedora, Centos Posts: 43,415 Rep: 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)
 11-28-2012, 05:41 AM #4 curious95 Member   Registered: Oct 2012 Location: /home/v Distribution: Slackware 14.0 Posts: 83 Original Poster Rep: understood, thanks for the answers