Lab-3
Lab-3
Aim: Use of conditional statements such as if else, for and while loop and
using switch case.
Software Used: MATLAB R2023b
Function / Command Used: Mathematical Operations, if elseif else, switch
case, for and while.
Programme (Code):
Ques 1
clc;
clear all;
a = input("enter coefficients of x^2")
b = input("enter the coeffieient of x")
c = input("enter the value of c")
D = b^2-4*a*c
if D>0
disp("roots are real")
elseif D==0
disp("roots are real and equal")
else
disp("roots are imaginary")
end
x_1=(-b+sqrt(D))/2*a
x_2=(-b-sqrt(D))/2*a
disp(x_1)
disp(x_2)
Output-
Ques-2
clc;
clear all;
switch dayNumber
case 1
disp("sunday")
case 2
disp("monday")
case 3
disp("tuesday")
case 4
disp("wednesday")
case 5
disp("thursday")
case 6
disp("friday")
case 7
disp("saturday")
otherwise
disp("invalid input. Enter no. between 1 and 7")
end
Output-
Ques –3
clc;
clear all;
x = input("enter first variable: ")
y = input("enter the second variable: ")
end
Output-
Ques 4
clc;
clear all;
Output-
Ques 5
clc;
clear all;
array = zeros(1,n);
sum_of_elements = 0;
for i = 1:n
array(i) = input("enter elements:");
sum_of_elements = sum_of_elements + array(i);
end
disp("The array is: ")
disp(array)
disp("Sum of elements of array is: ")
disp(sum_of_elements)
Output-
Ques-6
clc;
clear all;
n = input("enter the no. of students: ")
marks = zeros(1,10)
sum_of_marks = 0;
i = 1;
while i<=10
marks(i) = input("enter marks:")
sum_of_marks = sum_of_marks + marks(i);
i = i + 1;
end
disp("The marks is: ");
disp(marks);
disp("Sum of marks is: ");
disp(sum_of_marks);
Output-