Program that'll give a squared value of z

Gold Member
I'm writing a simple program that'll give a squared value of z, a number

Code:
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
double z;
cout<<"enter an integer"<<
cin>>z;>>
cout<<"The squared value of your integer is:"<<z*z<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}

my editor is telling me that it's wrong :|

and how would I make a program that can find a square root?

Staff Emeritus
#include<cmath>

pow(2.0,2.0) => 4.0
sqrt(4.0) => 2.0

Gold Member
and in the parnthesis i would put variable names?

faust9
yes:

pow(x,y) will yield the vaule of x to the power of y. You can use variables.

Gold Member
I put in this: cin>>x>>endl;

and it says: "x does not name a type"

ComputerGeek
dduardo said:
#include<cmath>

pow(2.0,2.0) => 4.0
sqrt(4.0) => 2.0

that is superfluous. you do not need the cmath library to do a simple square.

cin >> z;
z = z*z;
cout << z;

ComputerGeek
yomamma said:
I put in this: cin>>x>>endl;

and it says: "x does not name a type"

are you declaring your variables before you use them?

C/C++ is a typed language (not as strong as it could be, but it is)

you need a statement declaring the variable and the type before you use it.

int x;

Gold Member
okay, here's my code:

#include <cstdlib>
#include <iostream>
#include<cmath>

using namespace std;

int main(int argc, char *argv[])
{
int x;
cin>> x >>endl;
pow(x,2.0)
system("PAUSE")
return EXIT_SUCCESS;
}

Staff Emeritus
1) Why do you have >> endl; at the end of the cin statement? endl is equivalent to "\n".
2) What happened to the semicolons after pow(x,2.0) and system("PAUSE")?
3) Why aren't you outputting anything?

ComputerGeek
yomamma said:
okay, here's my code:

#include <cstdlib>
#include <iostream>
#include<cmath>

using namespace std;

int main(int argc, char *argv[])
{
int x;
cin>> x >>endl;
pow(x,2.0)
system("PAUSE")
return EXIT_SUCCESS;
}

You need to not use a endl in your input statement. That is an element of an output statement.

kaalen
Code:
#include <cstdlib>
#include <iostream>
#include <cmath>   //maybe you should also add whitespace here, I'm not sure

using namespace std;

int main(int argc, char *argv[])
{
int x;
cin>> x[COLOR=Red];[/COLOR]            // you don't have to add endl here because when you will enter some number and hit enter you will automatically jump into a new line... so no need for formatting here
pow(x,2.0)[COLOR=Red];[/COLOR]    //you have to add ";" behind every command/line
system("PAUSE")[COLOR=Red];[/COLOR]
return EXIT_SUCCESS;
}[COLOR=Red];[/COLOR]

faust9
Code:
#include <cstdlib>
#include <iostream>
#include <cmath>   //maybe you should also add whitespace here, I'm not sure

using namespace std;

int main(int argc, char *argv[])
{
int x;
cin >> x;            // you don't have to add endl here because when you will enter some number and hit enter you will automatically jump into a new line... so no need for formatting here
cout << pow(x,2.0);    //you have to add ";" behind every command/line
system("PAUSE");
return EXIT_SUCCESS;
};

Added a cout. No sense in calculating the power and not display or use it in some way, shape, or form.

Gold Member
I got it