Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

BPOPS103/203 Principles of Programming using C Lab

2.Compute the roots of a quadratic equation by accepting the coefficients. Print appropriate
messages.
Algorithm:
Step 1: START
Step 2 : Read coefficients a,b,c
Step 3: Compute the descriminant : d=(b*b)-(4*a*c)
Step 4: Check whether a==0 && b==0
if true, print invalid inputs
Step 5: Check whether a==0
if true, print linear equation
Compute root r1=-c/b
print Root=r1
Step 6: else if d==0
If true, print Roots are real and equal
Compute r1=-b/(2*a)
Compute r2=-b/(2*a)
print root r1 and r2
Step 7: else if d>0
If true, print roots are real and distinct
Compute r1=-b+(sqrt(d)/(2*a))
Compute r2=-b-(sqrt(d)/(2*a))
print root r1 and r2
Step 8: else if d<0
prints Roots are imaginary
Compute real r=-b/(2*a)
Compute imaginary i=(sqrt(fabs(d)))/(2*a)
print r1= real+i imaginary
print r2= real-i imaginary
Step 9: STOP

Dept. of AI & DS, SMVITM, Bantakal Page 1


BPOPS103/203 Principles of Programming using C Lab

Flowchart:

Dept. of AI & DS, SMVITM, Bantakal Page 2


BPOPS103/203 Principles of Programming using C Lab

Program:
#include<stdio.h>
#include<stdlib.h>
void main()
{
float num1, num2, result;
char op;
printf(“Enter the operator (+,-,*./) \n”);
scanf(“%c”, &op);
printf(“Please enter two numbers:\n”);
scanf(“%f %f”, &num1, &num2);
if(op = = ’+’ )
{
result = num1 + num2;
printf(“Result is %0.2f \n”, result);
}
else if(op = = ’-’ )
{
result = num1 - num2;
printf(“Result is %0.2f \n”, result);
}
else if(op = = ’*’ )
{
result = num1* num2;
printf(“Result is %0.2f \n”, result);
}
else if(op = = ‘/ ’ )
{
if(num2 = = 0)
{

printf(“Please enter non zero no. For num2: \n”);


exit(0);
}
else
{
result = num1 / num2; printf(“Result is %0.2f\n”,result);
}}
else
{
printf (“Entered operator is invalid \n”);
}} Simple Calculator

Dept. of AI & DS, SMVITM, Bantakal Page 3


BPOPS103/203 Principles of Programming using C Lab

Command to execute the Program:


$ cc lab1.c
$ ./a.out

OUTPUT:
Case 1: Enter the operator (+,-,*,/)
+
Please enter two numbers: 4 2
Result is 6.00

Case 2: Enter the operator (+,-,*,/)


-
Please enter two numbers: 4 2
Result is 2.00

Case 3: Enter the operator (+,-,*,/)


*
Please enter two numbers: 4 2
Result is 8.00

Case 4: Enter the operator (+,-,*,/)


/
Please enter two numbers: 4 2
Result is 2.00

Case 5: Enter the operator (+,-,*,/)


?
Please enter two numbers: 4 2
Entered operator is invalid

Case 6: Enter the operator (+,-,*,/)


/
Please enter two numbers: 4 0
Please enter non zero no. For num2

Dept. of AI & DS, SMVITM, Bantakal Page 4

You might also like