Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Program that'll give a squared value of z

  1. Sep 23, 2005 #1

    JamesU

    User Avatar
    Gold Member

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

    Code (Text):
    #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:
     
  2. jcsd
  3. Sep 23, 2005 #2

    dduardo

    User Avatar
    Staff Emeritus

    #include<cmath>

    pow(2.0,2.0) => 4.0
    sqrt(4.0) => 2.0
     
  4. Sep 23, 2005 #3

    JamesU

    User Avatar
    Gold Member

    and in the parnthesis i would put variable names?
     
  5. Sep 23, 2005 #4
    yes:

    pow(x,y) will yield the vaule of x to the power of y. You can use variables.
     
  6. Sep 24, 2005 #5

    JamesU

    User Avatar
    Gold Member

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

    and it says: "x does not name a type"
     
  7. Sep 24, 2005 #6
    that is superfluous. you do not need the cmath library to do a simple square.

    cin >> z;
    z = z*z;
    cout << z;
     
  8. Sep 24, 2005 #7

    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;
     
  9. Sep 24, 2005 #8

    JamesU

    User Avatar
    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;
    }
     
  10. Sep 24, 2005 #9

    dduardo

    User Avatar
    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?
     
  11. Sep 24, 2005 #10
    You need to not use a endl in your input statement. That is an element of an output statement.
     
  12. Sep 25, 2005 #11
    Code (Text):

    #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]
     
     
  13. Sep 25, 2005 #12
    Code (Text):


    #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.
     
  14. Sep 25, 2005 #13

    JamesU

    User Avatar
    Gold Member

    I got it :smile:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Program that'll give a squared value of z
  1. Looking for a program. (Replies: 2)

  2. USB Programming (Replies: 3)

  3. Genetic programming (Replies: 14)

  4. Graphics Programs? (Replies: 6)

Loading...