Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Discriminant calculator in C
- Author: Kyojin96
- Date: 9.11.2015
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- int main()
- {
- /* Variables */
- int a, b, c;
- float x1, x2, D, re, im;
- /* We need input for a, b & c */
- printf("a = "); scanf("%d", &a);
- printf("b = "); scanf("%d", &b);
- printf("c = "); scanf("%d", &c);
- /* if both equal 0 then it's degenerated */
- if(a == 0 && b == 0)
- {
- printf("The equation is degenerated\n");
- }
- /* in this case x = -c/b */
- else if(a == 0 && b != 0)
- {
- x1 = -c/b;
- printf("The equation has only one root x1 = %f\n", x1);
- }
- /* Check c first for optimisation */
- else if(c == 0 && a != 0 && b != 0)
- {
- x1 = -b/a;
- x2 = 0;
- printf("The equation has two roots x1 = %f and x2 = %f\n", x1, x2);
- }
- /* if all are bigger than 0 then calculate discriminant */
- else if(a != 0 && b != 0 && c != 0)
- {
- /* Use math funcs to power b a.k.a b^2 */
- float bPowered = pow(b, 2);
- D = bPowered - 4 * a * c;
- /* If discriminant is more than 0, it has 2 roots */
- if(D > 0)
- {
- x1 = (-b + sqrt(D))/(2 * a);
- x2 = (-b - sqrt(D))/(2 * a);
- printf("The equation has 2 roots x1 = %f and x2 = %f\n", x1, x2);
- }
- /* If it's less than 0, it has real and imaginary numbers */
- else if(D < 0)
- {
- re = -b/2*a;
- im = sqrt(fabs(D))/2*a;
- printf("The equation has real part re = %f and imaginery im = %f\n", re, im);
- }
- /* Else if it's 0 then it has two equal roots */
- else
- {
- x1 = -b/2*a;
- printf("The equation has two equal roots x1 = x2 = %f\n", x1);
- }
- }
- /* Pause the console app */
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement