Sunteți pe pagina 1din 5

RAGHU INSTITUTE OF TECHNOLOGY

SPECIFICATION:
(2)(b).C Program To find the roots of the quadratic equation
DESCRIPTION:
Nature of roots of quadratic equation can be known
from the Discriminate = b2-4ac
If b2-4ac >0 then roots are real and unequal
If b2-4ac =0 then roots are real and equal
If b2-4ac <0 then roots are imaginary
ALGORITHM:
Step 1: Start
Step 2: Read the a,b,c values
Srep 3: Compute d b2-4ac
Step 3: Check if d> 0 then
3.1 R 1 (-b+ pow((b*b-4*a*c),0.5))/2*a
3.2 R 2(-b-pow((b*b-4*a*c),0.5))/2*a
Step 3.3: else if check d 0 then
Step : 3.4 R1 R2 = -b/(2*a)
Step 5: Otherwise Print Imaginary roots
Step 6: Display Roots R1, R2
Step 7: Stop

FLOWCHART
Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

start

Read a,b,c

Dpow(b*b-4*a*c),0.5

false
d>0
true
false

R1((-b+D)/ (2*a)

D0

R2((-b-D)/ (2*a)
true

R1 
R2
-b/(2*a)

Display Roots R1
& R2

Print
Imaginary
ROOTS

STOP

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

PROGRAM
/* C Program to calculate the quadratic roots of the equation */
Program name:

// wk2b.c

/* Done By : C-Faculty

Dated: 15/10/2013*/

#include<stdio.h>

//

stdio.h imports i/p and o/p functions

#include<math.h>

//

math.h imports mathematical functions

int main()

// User defined program

// Main Program Begins

float a,b,c,r1,r2,d;
clrscr();

// Declare variables a, b ,c,r1,r2 and d


// Clears the output screen of the computer

printf("Enter the values for equation:"); // Displays the Quoted statement


scanf("%f%f%f",&a,&b,&c);

// Reads a,b and c values from the keyboard

/* check the condition */


if(a==0)
printf("Enter value should not be zero ");

// Displays the Quoted statement

else
{
d=b*b-4*a*c;

// Method for Calculating roots of a quadratic equation

/* check the condition */


if(d>0)
{
r1=(-b+sqrt(d)/(2*a)); r2=(-b-sqrt(d)/(2*a));

// Method-1 for Calculating roots R1 & R2

printf("roots are real and unequal\n"); printf("%f\n%f\n",r1,r2); // Displays the Quoted statement
}
else if(d==0)

// Check the condition

{
r1=-b/(2*a); r2=-b/(2*a);

// Method-2 for Calculating roots R1 & R2

printf("roots are real and equal\n");

// Displays the Quoted statement

printf("root=%f\n",r1); printf("root=%f\n",r2);

// Displays the Quoted statement

}
else

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

printf("roots are imaginary");

// Displays the Quoted statement

}
return(0);

// It returns the ouput

// Main Program Ends

PROCEDURE FOR EXECUTING THE PROGRAM:


Step 1: After typing the program, press ESC button+shift+: and then type wq(to save the program and
quit)
Step 2: Now compile the program by using the following command
cc wk2b.c lcurses -lm
Step 3: Now go for running the program by using the command
./a.out

Step 4: To create an executing file use the command


cc wk2b.c -curses o roots

EXPECTED I/P AND O/P:


Output (1)
Enter the values for equation: 1, 6, 9
Roots are real and equal
Root= -3.0000 Root= -3.0000
Output (2)
Enter the values for equation: 1, 2, 3 Roots are
imaginary.
ORIGINAL OUTPUT :
Output (1)
Enter the values for equation: 1, 6, 9
Roots are real and equal
Root= -3.0000 Root= -3.0000
Output (2)
Enter the values for equation: 2, 7, 6
Roots are real and unequal
Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

Root= -6.75
Root= -7.25

VIVA VOCE QUESTIONS:


1) What are various types of loop statements? Ans : While, dowhile, for loop statements

2) What is the difference between while and do-while statements?


Ans:

In while the condition will be checked first and then enter into a loop.
But in do- while the statements will be executed first and then finally check the Condition.

3) How to find the roots of qudratric equtations ?


Ans: Nature of roots of quadratic equation can be known from the quadrant = b2-4ac
If b2-4ac >0 then roots are real and unequal If b2-4ac =0 then
roots are real and equal If b2-4ac <0 then roots are imaginary

4) List out the C features ?


Ans: Portability,flexibility, wide acceptability etc..,
--xXx--

Department of Computer Science & Engg

S-ar putea să vă placă și