You are on page 1of 7

2.

Develop a program to solve simple computational problems using arithmetic


expressions and use of each operator leading to simulation of a commercial
calculator. (No built-in math function)
Algorithm:
Step 1: Start
Step 2: Read two values and an operator- a, b and operator
Step 3: Evaluate option code with case statements
Step 3.1: case ‘+’ result=a+b, goto step 4
Step 3.2: case ‘-‘ result=a-b, goto step 4
Step 3.3: case ‘*’ result=a*b, goto step 4
Step 3.4: case ‘/’ check if the denominator is 0
if true print divide by 0 error
goto step5
else
result = a/b
goto step 4
Step 3.5: case ‘%’ result =a%b
Step 3.6: default- Print the error message
Step 4: print the result
Step 5: Stop
Flowchart:

Start

Read operands and


an Operator

Switch
(operator)

Case ‘+’ Case ‘-’ Case ‘*’ Case ‘/’ Case ‘%’

res=a+b res=a-b res=a*b res=a%b


If F
(b==0)

Print Division by res=a/b


zero is not possible

Print the result

Stop
Program:

#include<stdio.h>
void main()
{
float a,b,res;
char op;
printf("Enter an Expression (eg:1+6): ");
scanf("%f %c %f",&a,&op,&b);
switch(op)
{
case '+' : res=a+b;
break;
case '-' : res=a-b;
break;
case '*' : res=a*b;
break;
case '/' : if(b==0)
{
printf("Arthimetic Exception:Cannot Divide a number by 0.");
return;
}
else
{
res=a/b;
}
break;
case '%' : res= (int)a % (int)b;
}
printf("%g %c %g = %g \n",a,op,b,res);
}
3. Develop a program to compute the roots of a quadratic equation by
accepting the coefficients. Print appropriate messages.
Algorithm:
Algorithm: To find and output all the roots of a given quadratic equation for
non-zero coefficients
Step 1: [Start]
Begin
Step 2: [Input the co-efficients of the quadratic equation]
Read a,b,c
Step 3:[Check for the non-zero coefficient of a]
If a==0 and b==0 then print “Invalid input”, go to step 2
Step 4: [Check for linear equation]
If a==0 then
Root =-c/b
print the root: root
Step 4: [Find the value of disc]
disc = b * b – 4 *a * c
Step 5: [Find the type and the values of roots of a given quadratic equation]

If (disc = 0) then
Print “The roots are equal” root1 = root2 = -b /2.0*a
print the roots: root1 and root2

Else If (disc > 0) then


Print “The roots are real and distinct”
root1 = (-b + sqrt(disc)) / 2.0*a
root2 = (-b - sqrt(disc)) / 2.0*a
print the roots: root1 and root2

else
Print “The roots are imaginary”
real = -b / 2.0*a
imaginary = sqrt(fabs(disc)) / 2.0*a
print the roots: root1 and root2 in x+iy form- real+iimaginary
Step 6: [Stop]
End
Flowchart:
Program:
#include<stdio.h>
#include<math.h>
void main( )
{
float a,b,c,disc,root1,root2,real,imag;
printf("Enter a,b,c values \n");
scanf("%f%f%f",&a,&b,&c);
disc =b*b - 4*a*c;
if( (a==0) && (b==0) )
{
printf("Invalid co-efficients \n");
}
else if(a==0)
{
printf("Linear equation \n");
root1= - c / b;
printf("Root=%f",root1);
}
else if(disc==0)
{
printf("The roots are Real and Equal \n");
root1= - b / (2*a);
root2= - b / (2*a);
printf("Root1=%f \n Root2=%f \n",root1,root2);
}
else if(disc>0)
{
printf("The roots are Real and distinct \n");
root1=( - b + sqrt(disc) ) / (2*a);
root2=( - b - sqrt(disc)) / (2*a);
printf("Root1=%f \n Root2=%f \n",root1,root2);
}
else
{
printf("The roots are Real and Imaginary \n");
real=- b / (2*a);
imag=sqrt( fabs ( disc ) ) / (2*a);
printf("Root1=%f + i %f \n",real,imag);
printf("Root2=%f - i %f \n",real,imag);
}

You might also like