Sqrt root

Write a program to calculate the following equation for any value of x (user input value):

sqrt (pow(x,3)+pow(x,2)-12*x-12)
Is this your homework?

You can include the <cmath> header so you can actually calculate the square root. I would start off something like this in terms of pseudo code

Declare a double to variable x and a variable result
Assign a value to x to initialise it like 0.0
Prompt the user using cout for a number they want to put into the formula
Use the cin command to get the number assigned to x
Assign the number you get for the equation to result
Use cout to display the answer for result
Faisal440 wrote:
Write a program to calculate the following equation for any value of x (user input value):
sqrt (pow(x,3)+pow(x,2)-12*x-12)

Try x = 0
Last edited on
Yes this is my homework and i try a lot but can't solve this.
Plz if any one can solve it help me plz.
I have to submit it tomorrow..plz solve it.i will be grateful to you
I have to writer it in a program
Last edited on
Hi,

I understand it is urgent, but people here don't really like doing other people's homework for them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <cmath>

int main()
{
    std::cout << "Hello World!\n";

   /*
     I have given examples for each step I have provided, use them to write your own

      Declare a double to variable x and a variable result (e.g. double y, sum)
      Assign a value to x to initialise it like 0.0 (e.g. y = 0.0)
      Prompt the user using cout for a number they want to put into the formula 
      (example - cout << "Text here")
      Use the cin command to get the number assigned to x
      (cin >> y)
      Assign the number you get for the equation to result
      (sum = 1 + y)
      Use cout to display the answer for result
       (cout << "The answer is = " << y << endl;)
   */

    return 0;
}


Leave yourself plenty of time to do your homework. I am actually a teacher and poor organisation is your enemy I am sure!
don't overthink it..
sqrt (pow(x,3)+pow(x,2)-12*x-12)
is correct.
or just say
complex<double> f(complex<double> x){return sqrt(x*x*x+x*x-12*x-12);}
if you don't want to deal with complex, you can check that the sqrt is positive and if not print 'imaginary result' or something.
Last edited on
You can use half the multiplications:

 
x * (x*x + x - 12) - 12

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    // VARIABLES
    double x = 0.0; // USER VALUE
    double y = 0.0; // ANSWER
    
    // PROMPT FOR INPUT FROM USER
    cout << "Please enter a number: " << endl;
    
    // GET VALUE FROM USER
    cin >> x;
    
    // PUT x IN EQUATION AND GET y
    y = sqrt (pow(x,3)+pow(x,2)-12*x-12);
    
    // PRINT ANSWER
    cout << "Answer: " << y << endl;
    
    return 0;
}


Please enter a number: 
10.2
Answer: 32.1068
Program ended with exit code: 0
Topic archived. No new replies allowed.