A]
OBJECTIVE:
To write and execute programs demonstrating Control Structures (If-Else, If-elseif-else, Select) using Scilab.
ALGORITHM:
Start the program.
Get the input from the user using input() method.
Use pmodulo() to find the positive remainder of the number.
Use pmodulo(number, 2) to determine if the number is divisible by 2 and check whether the number is odd or even.
Use the select statement to execute multiple cases.
dayNum returns the number corresponding to the system date (Sun = 1, Mon = 2, etc.).
dayString returns the day (Mon, Tue, etc.).
Handle different cases using select and display appropriate statements.
Check whether a number is positive, negative, or zero using if-elseif-else.
SOURCE CODE:
If-Else Program (Even/Odd Number Check):
a = input("Enter a number: ");
if pmodulo(a, 2) == 0 then
disp("Number is even");
else
disp("Number is odd");
end
SELECT STATEMENTS (Day of the Week):
[dayNum, dayString] = weekday(datenum());
select dayString
case "Mon" then
disp("Start of work week");
case "Tue" then
disp("Day 2");
case "Wed" then
disp("Day 3");
case "Thu" then
disp("Day 4");
case "Fri" then
disp("Last day of work week");
else
disp("Weekend");
end
If-elseif-else Condition (Positive/Negative/Zero Check):
number = input("Enter a number: ");
if number > 0 then
disp("positive");
elseif number < 0 then
disp("negative");
else
disp("Zero");
end
SAMPLE INPUTS & OUTPUTS:
If-Else Program:
// Run the following
exec('C:\Users\admin\Documents\if.sce', -1);
// Input: Enter a number: 5
// Output: Number is odd
exec('C:\Users\admin\Documents\if.sce', -1);
// Input: Enter a number: 6
// Output: Number is even
SELECT STATEMENTS:
exec('C:\Users\admin\Documents\select.sce', -1);
// Output: Start of work week
If-elseif-else condition:
exec('C:\Users\admin\Documents\nestedif.sce', -1);
// Input: Enter a number: 4
// Output: positive
exec('C:\Users\admin\Documents\nestedif.sce', -1);
// Input: Enter a number: 0
// Output: Zero
exec('C:\Users\admin\Documents\nestedif.sce', -1);
// Input: Enter a number: -7
// Output: negative
B]
OBJECTIVE:
To write and execute programs that demonstrate Control Structures (for, while, break, and continue) using Scilab.
ALGORITHM:
Start the program.
Get the input from the user.
Calculate the factorial using a for loop.
The same factorial calculation is done using a while loop.
Handle break and continue statements for sum calculations.
Sum all positive numbers entered by the user until 0 is entered, using break for termination and continue for invalid input.
SOURCE CODE:
FOR LOOP (Factorial Calculation):
function fact = factorial(n)
fact = 1;
for i = 1:n
fact = fact * i;
end
printf('Factorial of %d is %d\n', n, fact);
end
WHILE LOOP (Factorial Calculation):
function fact = factorial(n)
fact = 1;
i = 1;
while i <= n
fact = fact * i;
i = i + 1;
end
printf('Factorial of %d is %d\n', n, fact);
end
BREAK AND CONTINUE STATEMENTS (Sum of Positive Numbers):
a = 1;
sum = 0;
while a
n = input('Enter a number: ');
if n > 0 then
sum = sum + n;
elseif n < 0 then
disp('Enter a positive number.');
continue;
else
break;
end
end
printf('Sum of all positive numbers is %d\n', sum);
SAMPLE INPUTS & OUTPUTS:
For Loop (Factorial):
// Running factorial function
factorial(5);
// Output: Factorial of 5 is 120
While Loop (Factorial):
factorial(5);
// Output: Factorial of 5 is 120
BREAK AND CONTINUE STATEMENTS:
// Running the break and continue sum program
Enter a number: 2
Enter a number: -2
Enter a positive number.
Enter a number: 1
Enter a number: 0
// Output: Sum of all positive numbers is 3
RESULT:
The programs are executed successfully using If-Else, Select, If-elseif-else, For, While, and Break-Continue statements.