Hello, I am a new member to this forum. I have some announcements, I will continue to read some more.

I need to create a program were the user inputs a number, program displays the cube root for that number.

The code I got thusfar is:

#include <stdio.h>
#include <math.h>
int main(void)
{int cube;
do
{
printf("Please type in the number you wish to cube root: \n");
scanf_s("%d" , &cube);
double result;
result= pow( cube, 1.0 / 3.0 );
printf ("The cube root is " "%5.4f\n",result);
printf ("\n");
}while (cube !=-10094556);
return 0;

The program works, but when I type in a integer, for example (-2) it shows -1.#IND.

I tried changing int cube to double cube; but then it displays -1.#QNB

I am trying to resolve the issue listed above.

Just a something extra someone could be generous to help me with;

when I place a decimal it either crashes or goes in an endless loop.

THe loop currently placed in the program listed above lets the program loop until the user places that value.

It is a simple loop, if anyone has a different way of looping the program I would greatly appreaciate it.

Thank you to all who are willing to help or have tried.

And hopefully I am not breaking any rules, as I have not finished reading the announcements. :D

Do you have to allow negative input?

Edit:
BTW: You're getting the bad output because your app is dividing by zero most likely.

I would like it to allow negative numbers and decimals if possible; but mostly positive and negative numbers.

Are you serious?

My calculator gives me -1.235992105 as the cube root of -2

I am serious. There are three cube roots of -2 -- three values x for which x^3 = -2. I'm talking about complex numbers. Which one should be returned? The negative one? Or the two others which are sixty degree rotations of that? What about the sqrt(9999999999999999)/300000000 power of -2? What's that? You've got an irrational power that's really close to to 1/3. All of the sudden, you have countably infinite values x for which x ^ (300000000/sqrt(9999999999999999)) = -2. Which one do you choose? What about the pow(-2,1.0/3.0)? Remember that 1.0/3.0 is not one third. The floating point approximation will exactly represent some number in reduced form that has a denominator that is a power of two, greater than one. That means all the roots are not real.

If you want to approximate the function that's the inverse of the function f, where f(x) = x^3, you can't just use pow. You need to take the absolute value to the 1.0/3.0, and then adjust the sign.

If you were to ask me what pow(-2,1/3) should return, if it could return complex numbers, I'd say it should return the value equivalent to pow(2,1/3) * cos(pi/3) + i * pow(2,1/3) * sin(pi/3).

"If you want to approximate the function that's the inverse of the function f, where f(x) = x^3, you can't just use pow. You need to take the absolute value to the 1.0/3.0, and then adjust the sign."

Then lets say that when an user input the number as a negative, the program ignores the negative sign and does it as a positive (obviously using absolutbe value) and then at the end adding the "-" sign to the result.

How would I go about doing this, I am going to research on how to apply absolute value which hopefully is as simple as adding an ABS , but how would the program know when to display the "-" sign at the end if the number.

My new code:

#include <stdio.h>
#include <math.h>
int main(void)
 
{float cube;
printf("Please type in the number you wish to cube root: \n");
scanf_s("%f" , &cube);

double result;
result= pow(cube, 1.0 / 3.0 );
printf ("The cube root is " "%5.4f\n",result);
 
 
return 0;
}

It now allows decimals.
It does run, and shows the correct answer; but I keep getting an error:

1>Compiling...
1>Source.cpp
1>.\Source.c(11) : error C2143: syntax error : missing ';' before 'type'
1>.\Source.c(12) : error C2065: 'result' : undeclared identifier
1>Cube Root - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I can't seem to find a solution to this, the program compiles and runs but this error is just annoying me.

I still have not tried the absolute value ;) , will get back to that when I am done trying to correct this issue.

>It does run, and shows the correct answer; but I keep getting an error:
How can it run if it doesn't compile? (The reason it has errors is because you need to escape your quotes in the last printf statement.)

Ok, I fixed it by declaring it like this:

float cube,result;

instead of declaring separetely as a double.

Works!

Now back to the absolute value. lol

how exactly do I implement the:

pow(abs(n) ) *abs(n)/n

Also, if someone can please show me an effective really simple way to loop this program so the user does not have to quit and restart it to try other points.

I have been doing the:
do
while

method where the variable cannot be a specific number or else it escapes the program.


>how exactly do I implement the: pow(abs(n) ) *abs(n)/n
I'd say use an if() statement like Rashakil suggested. It makes your intentions much clearer. For the record, here's how you would do an if() statement:

// if it's 0 or more, we can just leave it as it is
    if (cube >= 0) {
        result= pow(cube, 1.0 / 3.0 );
    }
    // negative means we need to make it positive, then add '-' sign
    else {
        result= - pow(abs(cube), 1.0 / 3.0);
    }

If you want your loop to look more refined, use a do...while loop, but put a statement that says something like "Do it again y/n". Read the user input into a char, and then in the while() condition of the loop, check to see if that char is not set to 'y', in which case you would exit.

joeprogrammer you are the best;

I have learned alot from that sample code

I learned how to make commenting easier by using "//" instead of "/**"

to use the if else statement and the way you managed to display the (-) at the end.

I will continue practicing, but I thank you for your help. I thank all members who have helped me in this cube root program.

:)

Hello, I am a new member to this forum. I have some announcements, I will continue to read some more.

I need to create a program were the user inputs a number, program displays the cube root for that number.

The code I got thusfar is:

#include <stdio.h>
#include <math.h>
int main(void)
{int cube;
do
{
printf("Please type in the number you wish to cube root: \n");
scanf_s("%d" , &cube);
double result;
result= pow( cube, 1.0 / 3.0 );
printf ("The cube root is " "%5.4f\n",result);
printf ("\n");
}while (cube !=-10094556);
return 0;

The program works, but when I type in a integer, for example (-2) it shows -1.#IND.

I tried changing int cube to double cube; but then it displays -1.#QNB

I am trying to resolve the issue listed above.

Just a something extra someone could be generous to help me with;

when I place a decimal it either crashes or goes in an endless loop.

THe loop currently placed in the program listed above lets the program loop until the user places that value.

It is a simple loop, if anyone has a different way of looping the program I would greatly appreaciate it.

Thank you to all who are willing to help or have tried.

And hopefully I am not breaking any rules, as I have not finished reading the announcements. :D

You can't take the root of a negative number. What you are seeing on the screen is the error resulting from "sqrt(-X). Use this instead... sqrt(abs(-number)), It's finding the root of the absolute value, since you started with negative number, is imaginary "i".

Comments
Don't resurrect dead threads.

well since some one already kicked the dead horse ill join in. the guy is looking for the cubed root of a number. lets say its 27. the answer would be 3. now lets say its -27. it would be imaginary right? wrong it exist. if you take -3 and multiply it by -3 you get 9 and then you multiply by -3 again and presto you get -27. you are correct if it is the square root of a negative number but for cube roots there are real answers. FYI all odd roots you can have negative numbers and all even roots you cant.

This question has already been answered. Start a new discussion instead.