Program that'll give a squared value of z

  • Thread starter JamesU
  • Start date
  • #1
JamesU
Gold Member
750
3

Main Question or Discussion Point

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? :smile:
 

Answers and Replies

  • #2
dduardo
Staff Emeritus
1,898
3
#include<cmath>

pow(2.0,2.0) => 4.0
sqrt(4.0) => 2.0
 
  • #3
JamesU
Gold Member
750
3
and in the parnthesis i would put variable names?
 
  • #4
691
1
yes:

pow(x,y) will yield the vaule of x to the power of y. You can use variables.
 
  • #5
JamesU
Gold Member
750
3
I put in this: cin>>x>>endl;

and it says: "x does not name a type"
 
  • #6
379
0
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;
 
  • #7
379
0
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;
 
  • #8
JamesU
Gold Member
750
3
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;
}
 
  • #9
dduardo
Staff Emeritus
1,898
3
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?
 
  • #10
379
0
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.
 
  • #11
20
0
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]
 
  • #12
691
1
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.
 
  • #13
JamesU
Gold Member
750
3
I got it :smile:
 

Related Threads for: Program that'll give a squared value of z

Replies
2
Views
2K
Replies
14
Views
9K
  • Last Post
Replies
10
Views
977
  • Last Post
Replies
5
Views
6K
  • Last Post
Replies
17
Views
8K
  • Last Post
Replies
2
Views
3K
  • Last Post
Replies
4
Views
10K
  • Last Post
Replies
3
Views
5K
Top