0% found this document useful (0 votes)
109 views5 pages

Quadratic Equation Solver in C

This lab activity involves writing a C program to solve quadratic equations. The program uses if/else statements to determine if the roots are imaginary, real and different, or real and equal. It calculates the roots using the quadratic formula and prints the results to three decimal places. The objectives are to learn basic C functions like printf, scanf, sqrt and abs, and to use conditional statements like if and if-else. Figures in the document show example outputs for each case of roots. The conclusion is that the experiment helped learn how to use if/else statements and the difference between integer and float variables in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views5 pages

Quadratic Equation Solver in C

This lab activity involves writing a C program to solve quadratic equations. The program uses if/else statements to determine if the roots are imaginary, real and different, or real and equal. It calculates the roots using the quadratic formula and prints the results to three decimal places. The objectives are to learn basic C functions like printf, scanf, sqrt and abs, and to use conditional statements like if and if-else. Figures in the document show example outputs for each case of roots. The conclusion is that the experiment helped learn how to use if/else statements and the difference between integer and float variables in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

De La Salle University- Manila

Gokongwei College of Engineering

Lab Activity Number : 1


Lab Activity Title : Quadratic Equation Solver

Date Performed : July 18, 2020


Date Submitted : July 19, 2020

Instructor : Engr. Leonard U. Ambata

Name of Student : Cuevo, Alyssa Beatrice H.


Student ID : 11727357
Subject / Section : LBYEC72 / ER

Remarks:

______________________________________________________________________________
______________________________________________________________________________

Instructor’s Signature : ________________


I. Objectives:
1. Learn the basic of C-Language: printf(), scanf(), sqrt() and abs().
2. Learn the conditional statements of C-language using if and if-else.
3. Design, compile, test, run, and implement C language program.

II. Brief Introduction of the experiment

A quadratic equation is in the form ax + bx + c. the roots of the quadratic equation are given by
2

the following formula.

−𝑏 ± √𝑏 % − 4𝑎𝑐
2𝑎
(1)
The three cases [Eq.1,2,3] are discriminant values that below identify if the roots of a quadratic
equation are imaginary, real and different or real and the same. The if and if-else statement are
introduced to identify the type of root in the program. If statement is used in single alternative
structure where it executes only when the condition is true. On the other hand, If-else statement is
used in two alternative structures where it executes when the condition is either true or false.
𝑏 % − 4𝑎𝑐 < 0
(2)
%
𝑏 − 4𝑎𝑐 = 0
(3)
𝑏 % − 4𝑎𝑐 > 0
(4)
In summary, this experiment will write a program that calculates the solution of the quadratic
equation and will determine if the roots are: imaginary, real and the same or real and different.
III. Circuit/Data & Results/Graph
#include is a "preprocessor" directive that commands the compiler to
put code from the header called stdio.h which gives access to many
different functions

Float x1, x2, d-(determinant), a, b, c, Realroot


and Imaginaryroot are declared variables as float
because the values in the quadratic formula are
likely decimal.

%f was used to print real values with a decimal


point.

d(Determinant) determines the type of root

x1 and x2 solves for the roots of the


quadratic equation. This is particular
identifies the real roots when d>0

The sqrt() function was used to solve for the square


root term in the formula.1

The “==” operator tells us that the


first operand is equal to the second
operand

The abs() function was used to have an


absolute value of the equal root value x1

This identifies the roots:


Imaginary(x2) and real(x1)

Figure 1. Quadratic equation solver program with notes


Figure 2. Outcome of “Real Roots and Different”

Figure 3. Outcome of “Real Roots and Equal”

Figure 4. Outcome of “Imaginary Roots”

IV. Analysis & Conclusion

In this experiment, the C++ language program given its operators and functions used were
mastered by writing a program that would solve a quadratic equation. To further discuss,
determinants solve what type of roots there are in the equation inputted. Determinants have three
cases, Equation 2 solves for the real roots with different values [Fig. 2], Equation 3 solves for equal
roots that are equal [Fig. 3], and Equation 4 solves for the imaginary roots [Fig. 4]. All values have
3 decimal places due to the “%.3f” operator used. In order for this identification to be possible,
the if and if-else statements were used in two alternative structures where it executes when the
condition is either true or false. In this experiment, if statement is used for determinant values
greater than 0, else if statement is used with the guidance of the “==” operator. This tells the
program that the first operand, being “d” is equal to the second operand, being 0. An additional
else statement was used for the last case; determinant is less than 0. This gives the imaginary roots
of the equation inputted. In conclusion, the experiment showed a brief introduction of the if and
if-else statements and to master the difference between using the integer and float variables. One
thing to commend is the detail in the program solver, it not only tells you whether the roots are
real or imaginary, it also says if it contains real roots with different values or real roots with equal
values. The use of the abs() function may have made writing the program easier.

You might also like