LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 11-28-2012, 01:14 AM   #1
curious95
Member
 
Registered: Oct 2012
Location: /home/v
Distribution: Slackware 14.0
Posts: 83

Rep: Reputation: Disabled
Post 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)
 
Old 11-28-2012, 02:07 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
There's the function pow() (and associated powf() and powl()) in math.h.
 
Old 11-28-2012, 02:07 AM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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)
 
Old 11-28-2012, 05:41 AM   #4
curious95
Member
 
Registered: Oct 2012
Location: /home/v
Distribution: Slackware 14.0
Posts: 83

Original Poster
Rep: Reputation: Disabled
Post

understood, thanks for the answers
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script to created folders based on multiple input files prravin1 Programming 1 05-04-2012 06:42 AM
[SOLVED] advice required to do a replace task based on input criteria contra04 Programming 7 01-17-2012 03:38 PM
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 06:40 PM
User based Authentication in Squid instead of Terminal based. TSK2000 Linux - Software 1 12-30-2005 02:22 AM
User powers!? SCX Debian 6 09-04-2004 06:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration