Skip to content

Instantly share code, notes, and snippets.

@mmikhan
Created March 5, 2015 18:35
Show Gist options
  • Save mmikhan/3c335892e4fb6ae874f9 to your computer and use it in GitHub Desktop.
Save mmikhan/3c335892e4fb6ae874f9 to your computer and use it in GitHub Desktop.
Write a program to compute the real roots of a quadratic equation
//
// main.c
// assignment
//
// Created by Md Mazedul Islam Khan on 3/6/15.
// Copyright (c) 2015 Md Mazedul Islam Khan. All rights reserved.
//
#include <stdio.h>
#include <math.h>
int main(int argc, const char * argv[]) {
float a, b, c, eqn, eq1, eq2, real, roots;
printf("Please insert the value of a, b and c:\n");
scanf("%f %f %f", &a, &b, &c);
eqn = (b * b) - (4 * a * c);
if (eqn > 0) {
eq1 = -b + sqrt(eqn) / (2 * a);
eq2 = - b - sqrt(eqn) / (2 * a);
} else if (eqn == 0) {
eq1 = eq2 = - b / (2 * a);
printf("Roots are: %.2f and %.2f", eq1, eq2);
} else {
real = - b / (2 * a) ;
roots = sqrt(- eqn) / (2 * a);
printf("Roots are: %.2f and %.2f", real, roots);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment