![]() |
| | #1 |
| Registered User Join Date: Jan 2009
Posts: 4
| Squaring numbers in c++ I am trying to make a calculator in C++. I am stuck on how to square a number. Would anybody know how to do this? |
| Sanka792 is offline | |
| | #2 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,372
| You er... multiply the number by itself.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is online now | |
| | #3 |
| Registered User Join Date: Jan 2009
Posts: 4
| |
| Sanka792 is offline | |
| | #4 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,372
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way Last edited by laserlight; 01-27-2009 at 12:31 PM. Reason: oafter -> after | |
| laserlight is online now | |
| | #5 |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,744
| Google is your friend
__________________ My homepage Advice: Take only as directed - If symptoms persist, please see your debugger |
| iMalc is offline | |
| | #6 |
| Registered User Join Date: Jan 2009
Posts: 4
| coudl you give an example of using this function. For example how to find the square root of 16? |
| Sanka792 is offline | |
| | #7 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,372
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
| | #8 | |
| The larch Join Date: May 2006
Posts: 3,222
| Um, it takes a value (16) and returns the square root of the value (4.0). There's nothing more to it. Do you have an idea how to call any function?
__________________ I might be wrong. Quote:
| |
| anon is offline | |
| | #9 |
| Registered User Join Date: Jun 2006
Posts: 83
| Code: #include <iostream>
#include <cmath>
int main(){
int n = 16; // number to square root
std::cout << std::sqrt(n) << std::endl;
}
|
| Kudose is offline | |
| | #10 |
| Student Join Date: Aug 2008 Location: UK -> Newcastle
Posts: 156
| Well from where i'm sitting, it sounds like a big function to me. I havn't made a calc persae, but i'll probably make one now just for the fun of it. The way I would do it would be trial and error. Think about the behaviour of numbers. An odd number multiplied by an odd number is obviously going to be an odd number. Start from there, create a method of finding out if the input number was even or odd, then take it from there. |
| legit is offline | |
| | #11 |
| Registered User Join Date: Jan 2009
Posts: 4
| Thnks guys so much for the help. I now know what to do. I already have a prototype that can add, subtract, multiply and divide. But i was thinking of adding in formulas like the -b formula. But i goit another question. Is it possible to create an interface for c++ so that it looks like a program? I mean a console that a user can use that covers up command prompt? |
| Sanka792 is offline | |
| | #12 | |
| Student Join Date: Aug 2008 Location: UK -> Newcastle
Posts: 156
| Quote:
| |
| legit is offline | |
| | #13 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,372
| Quote:
At the moment I would suggest a "classical" problem: allow the user to enter an arithmetic expression, from the console window, in reverse Polish notation (search the Web if you must). When you are satisfied with what you have done, allow the user to enter an arithmetic expression in the "normal" infix notation. You could always reuse this later on when you do write a GUI calculator.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
| | #14 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| Just a note: if you wanted to raise a number to an arbitrary power, you can use the pow() function, also in <cmath>.
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, nort, etc. |
| dwks is offline | |
![]() |
| Tags |
| c++, calculator |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Question about random numbers | Kempelen | C Programming | 2 | 07-02-2008 06:28 AM |
| Help with Rational Numbers (C++) | cloudjc | C++ Programming | 3 | 04-28-2008 04:03 PM |
| Logical errors with seach function | Taka | C Programming | 4 | 09-18-2006 05:20 AM |
| the definition of a mathematical "average" or "mean" | DavidP | A Brief History of Cprogramming.com | 7 | 12-03-2002 11:15 AM |
| A (complex) question on numbers | Unregistered | C++ Programming | 8 | 02-03-2002 06:38 PM |