for loop
>>sum=0;
>>for k=[1 5 7 6];
sum=sum+k
end;
sum =
UNIT 2
1
sum =
6
sum =
13
sum =
19
>> %sum, avg. are sum and average of variables
respectively
>>sum=0;
>>for i=1:10
n= input( ' Enter the value:');
sum=sum+n;
UNIT 2
end;
Enter the value:3
Enter the value:5
Enter the value:24
Enter the value:5
Enter the value:6
Enter the value:4
Enter the value:10
Enter the value:23
Enter the value:45
Enter the value:2
>>avg=sum/10;
>>fprintf(' The average is :%f', avg);
The average is :12.700000
UNIT 2
The file CSah1.m
for a= 0:2:4
b=0;
value=0;
for b= 1:3
value= value+a+b;
fprintf(' The Inner loop: \n');
end;
fprintf(' The outer loop: \n');
fprintf('a= %d \t b= %d\t value = %d',a,b,value);
end;
The output on command window:
>> CSah1
UNIT 2
The Inner loop:
a= 0 b= 1
The Inner loop:
a= 0 b= 2
The Inner loop:
a= 0 b= 3
The outer loop:
a= 0 b= 3 value = 6
The Inner loop:
a= 2 b= 1
The Inner loop:
a= 2 b= 2
The Inner loop:
a= 2 b= 3
UNIT 2
The outer loop:
a= 2 b= 3 value = 12
The Inner loop:
a= 4 b= 1
The Inner loop:
a= 4 b= 2
The Inner loop:
a= 4 b= 3
The outer loop:
a= 4 b= 3 value = 18
>>
UNIT 2
while loop
The function csah2.m
function csah2
%Program to add given number till the number
%entered is 0
sum=0; % initialize the variable sum
UNIT 2
num = input (' Enter the first number ');
fprintf ('Enter 0 to end the summation \n');
while (num ~= 0),
sum=sum+num;
num= input (' Enter the next number ');
end;
fprintf ('The sum of numbers till ''0'' is enterd is
The output on command window:
>> csah2
Enter the first number 34
Enter 0 to end the summation
Enter the next number 45
Enter the next number 6
UNIT 2
Enter the next number 5
Enter the next number 6
Enter the next number 5
Enter the next number 60
Enter the next number 0
The sum of numbers till '0' is enterd is 161
If loop
%Program to check whether number greater less
%or equal to 50 and saved in the file CSah3.m
disp(' To check whether number greater less or
equal to 50');
x= input('Enter a number :');
UNIT 2
if x>50,
disp('The number is greater than 50');
elseif (x==50)
disp('The number is equal to 50');
else
disp('The number is smaller than 50');
end
>>CSah3
To check whether number greater less or equal
to 50
Enter a number :56
The number is greater than 50
>> CSah3
UNIT 2
To check whether number greater less or equal
to 50
Enter a number :10
The number is smaller than 50
%This program finds largest of three number and
%saved in the file CSah4.m
x1= input('enter first number:');
x2= input('enter second number:');
x3= input('enter third number:');
if (x1>x2),
UNIT 2
if(x1> x3),
fprintf(' The largest number is %d\n', x1)
end;
elseif (x2>x3),
fprintf(' The largest number is %d\n', x2)
else
fprintf(' The largest number is %d\n', x3)
end;
>> CSah4
enter first number:45
enter second number:98
enter third number:34
The largest number is 98
UNIT 2
switch – case
% Program to find multiple of 2 and 3 from first %
% 15 numbers
% Using switch- case statement saved in the file
% CSah4.m
number= input('Enter a number between 1 and
UNIT 2
15:');
switch number,
case {6 12}
fprintf (' The selected number is multiple of 2 and
3\n');
case {2 4 6 10 12 14},
fprintf (' The selected number is multiple of 2
case {3 6 9 12 15}
fprintf (' The selected number is multiple of
3\n');
otherwise
fprintf (' The selected number is neither multiple
of 2 and nor 3\n');
UNIT 2
end;
>> CSah5
Enter a number between 1 and 15:9
The selected number is multiple of 3
>> CSah5
Enter a number between 1 and 15:6
The selected number is multiple of 2 and 3