Monday, 18 November 2013

Study and Implementation of Branching Statements (if – else and Switch). Implementation of Finding Quadratic equation, Finding Even odd, Finding Greatest Number....

AIM: Study and Implementation of Branching Statements (if – else and Switch). 
THEORY:
Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

1. If Statement:

Syntax:
If (condition)
Statement;
Statement;           
end
2. if – else Statement:
An if statement can be followed by an optional else statement, which executes when the boolean expression is false.

Syntax:
If (condition)
Statement;
Statement;           
Else
Statement;
Statement;           
end

3. Switch case:
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.

Syntax:
Switch (Control Expression)
            Case 1
            Statement;
            Case 2
            Statement;
            ….
….
….
Case n
Statement;
Otherwise
Statement;
end

PROCEDURE:
1. Program for Even and Odd number:
            Using ‘mod’ command find mod (number, 2). If output value is 1 then given number is odd number otherwise number is even number. Use if – else statement.
2. Program for Greatest number among three numbers:
            This can be achieved using relational and logical operators.
If (num1>num2 && num1>num3) then num1 is greatest number otherwise check for second number.
3. Program for finding roots of Quadratic equation:
            If (b^2-4ac) < 0 then it has two complex roots. If (b^2-4ac) = 0 then it has two equal real roots and if (b^2-4ac) > 0 then it has two distinct roots. Using if – else implement all these equations.

4. Program using Switch case:
            If case 1 is true then do addition, if case 2 is true then do subtraction, if case 3 is true then do multiplication and if case 4 is true then do division of two numbers.

1. Program for Even and Odd:
Program:
%Program for Even odd Number
a=input('Enter Number for Even or Odd = ');
if(mod(a,2)==0)
    disp('Number is Even')
else
    disp('Number is Odd')
end
Output:
Enter Number for Even or Odd = 5
Number is Odd
Enter Number for Even or Odd = 6
Number is Even

2. Program for greatest number:
Program:
% Program for finding Greatest number between three numbers
a=input('Enter 1st Number = ');
b=input('Enter 2nd Number = ');
c=input('Enter 3rd Number = ');

if(a>b && a>c)
    disp('Greatest Number is =');a
elseif(b>a && b>c)
    disp('Greatest Number is =');b
else
    disp('Greatest Number is =');c
end

Output:
Enter 1st Number = 3
Enter 2nd Number = 10
Enter 3rd Number = 4
Greatest Number is =
b =10

3. Program for Quadratic Equation
Program:
% Prompt the user for the coefficients of the equation
disp ('This program solves for the roots of a quadratic ');
disp ('equation of the form A*X^2 + B*X + C = 0. ');
a = input ('Enter the coefficient A: ');
b = input ('Enter the coefficient B: ');
c = input ('Enter the coefficient C: ');
% Calculate discriminant
discriminant = b^2 - 4 * a * c;
% Solve for the roots, depending on the value of the discriminant
if discriminant > 0 % there are two real roots, so...
x1 = ( -b + sqrt(discriminant) )/( 2 * a );
x2 = ( -b - sqrt(discriminant) )/( 2 * a );
disp ('This equation has two real roots:');
fprintf ('x1 = %f\n', x1);
fprintf ('x2 = %f\n', x2);
elseif discriminant == 0 % there is one repeated root, so...
x1 = ( -b )/( 2 * a );
disp ('This equation has two identical real roots:');
fprintf ('x1 = x2 = %f\n', x1);
else % there are complex roots, so ...
real_part = ( -b )/( 2 * a );
imag_part = sqrt ( abs ( discriminant ) )/( 2 * a );
disp ('This equation has complex roots:');
fprintf('x1 = %f +i %f\n', real_part, imag_part );
fprintf('x1 = %f -i %f\n', real_part, imag_part );
end
Output:
This program solves for the roots of a quadratic
equation of the form A*X^2 + B*X + C = 0.
Enter the coefficient A: 1
Enter the coefficient B: 5
Enter the coefficient C: 6
This equation has two real roots:
x1 = -2.000000
x2 = -3.000000
4. Program for Switch case:
Program:
% Switch Case

a=input('Enter 1st number = ');
b=input('Enter 2nd number = ');
choice=input('Enter Choice = ');

switch(choice)
    case 1
        c=a+b;
        disp('Addition is');c
    case 2
        c=a-b;
        disp('Subsrtaction is');c
    case 3
        c=a*b;
        disp('Multiplication is');c
    case 4
        c=a/b;
        disp('Division is');c
    case 5
        c=(a+b)/2;
        disp('Average is');c
    otherwise
        disp('Option is Invalid')
end
Output:
Enter 1st number = 3
Enter 2nd number = 6
Enter Choice = 4
Division is
c = 0.5000
Enter 1st number = 4
Enter 2nd number = 8
Enter Choice = 9
Option is Invalid

No comments:

Post a comment