Home | Lab 1 | Lab 2 | Lab 3 | Lab 4 | Lab 5 | Lab 6 | Lab 7 | Lab 8 | Lab 9 | Lab 10 | Lab 11 | Lab 12

Linked at EntranceNepal.com, i-geek blog & Ekendra's Share World

Object: I. To write a program to calculate roots of a quadratic equation ie ax2+bx+c=0.

Background Theory:

Like most languages C uses keywords to implement the decision control instruction. The general form of if statement looks like:

if (this condition is true)

execute this statement;

The keyword tells the compiler that what follows, is a decision control instruction. The condition following the key word if is always enclosed in parentheses. If the condition, whatever it is, is true, then the statement is executed. If the statement is not true the statement is not executed; instead the program skips past it.

The if  statement

The if statement has the same function as other languages. It has three basic forms, among which given sort is used to execute the given object.

`  if (expression)`
`                         statement1`
`             else if (expression)`
`                         statement2`
`             else`
`                         statement3`

Object Theory:

First step to write the object is to find what lies inside the quadratic equation ie ax2+bx+c=0. It has two roots & , depending upon the nature of determinant √d=√(b2-4ac) there may exist three sorts of root pair as given:

I.      √(b2-4ac)>0 :    Real and unequal

II.      √(b2-4ac)=0 :    Real and equal

III.      √(b2-4ac)<0 :    Imaginary and conjugate

Also it should be noted that real roots are equal and imaginary roots in case three defer only in sign to say (x2) imaginary = (x1)imaginary.

Code:

// Program to calculate roots of a quadratic equation ie ax2+bx+c=0 //

# include <stdio.h>

# include <conio.h>

#include <math.h>

void main()

{

float a, b, c, d, x1,x2;

clrscr();

printf("Enter coefficients of quadratic equation ie a, b and c: ");

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

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

if (d>0)

{

x1= (-b+(sqrt(d)))/(2*a);

x2= (-b-(sqrt(d)))/(2*a);

printf("Roots x1= %f and x2= %f are real and unequal", x1, x2);

}

else if (d==0)

{

x1= (-b/2*a);

x2= x1;

printf("Roots x1= %f and x2= %f are  real and equal", x1, x2);

}

else

{

x1= (-b/2*a);

x2= sqrt(abs(d))/(2*a);

printf("Real part= %f and imaginary part= %f are complex conjugates", x1,x2);

}

printf(“\n\n\t\t Press any key to continue…”);

getch();

}

Search yourself for more such codes

Home | Lab 1 | Lab 2 | Lab 3 | Lab 4 | Lab 5 | Lab 6 | Lab 7 | Lab 8 | Lab 9 | Lab 10 | Lab 11 | Lab 12