Program that'll give a squared value of z

In summary, the conversation was about writing a program to calculate the squared value of a number, and then also discussing how to find a square root using the cmath library in C++. The expert summarized the code needed for the program and provided tips on how to properly declare and use variables, add semicolons, and format the code. They also emphasized the importance of outputting the result of the calculation.
  • #1
JamesU
Gold Member
815
3
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:
 
Computer science news on Phys.org
  • #2
#include<cmath>

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

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

and it says: "x does not name a type"
 
  • #6
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
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
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
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
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
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
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
I got it :smile:
 

1. How does the program calculate the squared value of z?

The program uses the formula z^2 to calculate the squared value of z. This means that the program multiplies z by itself to get the squared value.

2. Can the program handle negative values of z?

Yes, the program can handle negative values of z. The squared value of a negative number is still positive, so the program will return a positive result.

3. Is there a limit to the value of z that the program can handle?

It depends on the programming language and data type used. In general, most programming languages can handle large values of z, but there may be limitations for extremely large numbers.

4. Can the program be used for complex numbers?

It depends on the specific program and its capabilities. Some programs may have the ability to handle complex numbers, while others may only work with real numbers. It is best to check the program's documentation or features list to see if it supports complex numbers.

5. How accurate is the program's calculation of the squared value of z?

The accuracy of the program's calculation depends on the precision of the data type used and the limitations of the programming language. In general, most programming languages have high precision and can accurately calculate the squared value of z.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
754
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
840
  • Programming and Computer Science
Replies
30
Views
2K
Back
Top