I am trying to figure out how to display the square root of a number if it happens to be negative (as it is entered by the user), and if so, display it correctly with the "i" displayed as well. When I do the normal sqrt
function, the result is always something like -1.#IND. When I tried using the double complex variables, the positive numbers nor the negative numbers would come out clean.
Below is my code; the comments are what my goal is. The 4 num variables are entered by the user and can be any integer, positive or negative.
// Display the square root of each number. Remember that the user can enter negative numbers and
// will need to find the negative root with the "i" displayed.
printf("
The square root of %d is %.4f", num1, sqrt(num1));
printf("
The square root of %d is %.4f", num2, sqrt(num2));
printf("
The square root of %d is %.4f", num3, sqrt(num3));
printf("
The square root of %d is %.4f", num4, sqrt(num4));
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…