1. 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. You er... multiply the number by itself.

3. Originally Posted by laserlight
You er... multiply the number by itself.
oke and how do you square root a number?

4. 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.

6. Originally Posted by laserlight
You er... multiply the number by itself.
Originally Posted by laserlight
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. 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.

8. 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?

9. 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. Originally Posted by Sanka792
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. 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. Originally Posted by Sanka792
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. 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.

14. Just a note: if you wanted to raise a number to an arbitrary power, you can use the pow() function, also in <cmath>.