Squaring numbers in c++

This is a discussion on Squaring numbers in c++ within the C++ Programming forums, part of the General Programming Boards category; hey people! I am trying to make a calculator in C++. I am stuck on how to square a number. ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    4

    Squaring numbers in c++

    hey people!
    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?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    You er... multiply the number by itself.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    4
    Quote Originally Posted by laserlight View Post
    You er... multiply the number by itself.
    oke and how do you square root a number?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    Quote Originally Posted by Sanka792
    oke and how do you square root a number?
    That depends on how you want to do it, but one way is to use the std::sqrt function from <cmath>. That said, it would be wiser to start by implementing say, addition, and only implement other functionality after you have a working prototype.
    Last edited by laserlight; 01-27-2009 at 11:31 AM. Reason: oafter -> after
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    Google is your friend
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    4
    Quote Originally Posted by laserlight View Post
    You er... multiply the number by itself.
    Quote Originally Posted by laserlight View Post
    That depends on how you want to do it, but one way is to use the std::sqrt function from <cmath>. That said, it would be wiser to start by implementing say, addition, and only implement other functionality oafter you have a working prototype.
    coudl you give an example of using this function. For example how to find the square root of 16?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    Quote Originally Posted by Sanka792
    coudl you give an example of using this function. For example how to find the square root of 16?
    You could read say, cppreference.com's entry on sqrt, and/or you could search the Web as iMalc suggested.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    Code:
    #include <iostream>
    #include <cmath>
    
    int main(){
      int n = 16; // number to square root
      std::cout << std::sqrt(n) << std::endl;
    }
    This was not compiled and just getting the answer doesn't do you any good.

  10. #10
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Quote Originally Posted by Sanka792 View Post
    coudl you give an example of using this function. For example how to find the square root of 16?
    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.

  11. #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?

  12. #12
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Quote Originally Posted by Sanka792 View Post
    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?
    You mean like a Window? This is called a GUI(Graphical User Interface). Yes this is possible, but hard work. I myself cannot get the hang of it, although poeple on the boards will gladly help you.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    Quote Originally Posted by Sanka792
    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?
    As in a graphical user interface that say, resembles a hand held calculator? Yes, but since you appear relatively new to C++... not yet.

    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
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 06:28 AM
  2. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 04:03 PM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 10:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21