I'm making a program that will calculate the discriminant of a quadratic and displays the number of roots based on whether the result is positive, negative, or zero. However, it will only display 'two roots' when that case is satisfied, and nothing else when the cases are supposed to display 'one root' or 'no roots'.
Here is my code:
Code: Prompt A, B, C
B^2 - 4AC→X
If X > 0
Disp "TWO ROOTS"
If X = 0
Disp "ONE ROOT"
Else
Disp "NO ROOTS"
End
Help please?
You forgot the "Then" which is needed for doing If Else or multi line If's
Code: Prompt A, B, C
B^2 - 4AC→X
If X > 0
Disp "TWO ROOTS"
If X = 0
Then
Disp "ONE ROOT"
Else
Disp "NO ROOTS"
End
Thank you! However, I changed it to this
Code: Prompt A, B, C
B^2 - 4AC→X
If X > 0
Disp "TWO ROOTS"
If X = 0
Then
Disp "ONE ROOT"
If X < 0
Disp "NO ROOTS"
End
because every time I did a case where X was greater than zero, it displayed both "TWO ROOTS" and "NO ROOTS". But it still doesn't display anything for the case X < 0, although it works for the other cases.
Try This:
Code: Prompt A, B, C
B^2 - 4AC→X
If X > 0
Then
Disp "TWO ROOTS"
Else
If X = 0
Then
Disp "ONE ROOT"
Else
Disp "NO ROOTS"
End
End
It might help to look at it with a bit of indention:
Code: Prompt A, B, C
B^2 - 4AC→X
If X > 0
Disp "TWO ROOTS"
If X = 0
Then
Disp "ONE ROOT"
If X < 0
Disp "NO ROOTS"
End
This means that, when X = 0, then it displays "ONE ROOT" and then checks if X < 0. But, because the If statement evaluated to true, then we know that X is not less than 0, and can never be. Here is Jonimus' with indention:
Code: Prompt A, B, C
B^2 - 4AC→X
If X > 0
Then
Disp "TWO ROOTS"
Else
If X = 0
Then
Disp "ONE ROOT"
Else
Disp "NO ROOTS"
End
End
It works! Thank you so much!
I thought I would make some optimisations:
Code:
Prompt A,B,C
B^2-4AC
If not(Ans
Disp "ONE ROOT
If Ans>0
Disp "TWO ROOTS
If Ans<0
Disp "NO ROOTS
This isn't as optimised as it could be, but it is still easy enough to follow, I think. To complicate things a little:
Code:
Prompt A,B,C
ClrHome
B^2-4AC
(Ans≥0)+(Ans>0
Output(1,1,Ans
Output(1,3,"ROOT
If Ans≠1
Ouput(1,7,"S
Note: For whatever reason, the 'squared' symbol isn't allowed in a post, so I replaced them with ^2.
The number of real roots can be given by: 1+tanh(E9(B²-4AC
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.
»
Go to Registration page
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum