MATLAB Bal Kishor Premier Academy BASIC MATLAB
Program
MATLAB Basic Programs
30 April 2020
Simple MATLAB programs in MATLAB
Use of user input, if, elseif, if…else, switch case, disp, and basic
programming skills
To print, we can use any of the following methods:
fprintf('This is an statement.\n');
disp('This is an statement.');
x = 10;
fprintf('%d is the value of x.\n');
disp([num2str(x), ' is the value of x.]);
While using fprintf, %d→ for integers, %s for string etc
While using disp, → we must convert the variable to string using
num2str function to display the value.
Program 1
% 1. Write a MATLAB program to
% find maximum between two numbers
% entered by the user.
clc
close all
clear all
x = input('Enter number1:');
y = input('Enter number2:');
if x>y
disp(['Maximum = ',num2str(x)])
elseif y>x
disp(['Maximum = ',num2str(y)])
else
disp('Both are same number')
end
Output:
Enter number1:10
Enter number2:20
Maximum = 20
MALAB Tutorials 02 +91 8095558063
[email protected],
[email protected] www.bkpacademy.com
MATLAB Bal Kishor Premier Academy BASIC MATLAB
Program
Program 2
% 2. Write a MATLAB program to
% find maximum between three
% numbers entered by user.
clc
close all
clear all
num1 = input('Enter number1:');
num2 = input('Enter number2:');
num3 = input('Enter number3:');
if num1>num2 && num2>num3
disp(['Maximum = ',num2str(num1)])
elseif num2>num1 && num1>num3
disp(['Maximum = ',num2str(num2)])
else
disp(['Maximum = ',num2str(num3)])
end
Output:
Enter number1:20
Enter number2:30
Enter number3:10
Maximum = 30
Program 3
% 3. Write a MATLAB program to
% check whether a number entered
% by user is negative, positive or
% zero.
clc
close all
clear all
num1 = input('Enter number1:');
if num1>0
disp([num2str(num1),' is positive'])
elseif num1==0
disp([num2str(num1),' is zero'])
else
disp([num2str(num1),' is negative'])
end
Output:
Enter number1:3
3 is positive
Enter number1:-2
-2 is negative
Enter number1:0
0 is zero
MALAB Tutorials 02 +91 8095558063
[email protected],
[email protected] www.bkpacademy.com
MATLAB Bal Kishor Premier Academy BASIC MATLAB
Program
Program 4
% 4. Write a MATLAB program to
% check whether a number is
% divisible by 5 and 11 or not.
clc
close all
clear all
num1 = input('Enter number1:');
if mod(num1, 5)==0 && mod(num1,11)==0
disp([num2str(num1),' is divisible by 5 and 11.'])
else
disp([num2str(num1),' is not divisible by 5 and/or 11.'])
end
Output:
Enter number1:55
55 is divisible by 5 and 11.
Enter number1:44
44 is not divisible by 5 and/or 11.
Program 5
% 5. Write a MATLAB program to
% check whether a number entered
% by user is even or odd.
clc
close all
clear all
num1 = input('Enter number1:');
if mod(num1, 2)==0
disp([num2str(num1),' is even.'])
else
disp([num2str(num1),' is odd.'])
end
Output:
Enter number1:20
20 is even.
Enter number1:7
7 is odd.
MALAB Tutorials 02 +91 8095558063
[email protected],
[email protected] www.bkpacademy.com
MATLAB Bal Kishor Premier Academy BASIC MATLAB
Program
Program 6
% 6. Write a MATLAB program to
% check whether a year entered by
% user is leap year or not.
% We have, A leap year is exactly divisible by 4 except for century
years
% (years ending with 00). The century year is a leap year only if
% it is perfectly divisible by 400.
clc
close all
clear all
num1 = input('Enter an year:');
if mod(num1, 4)==0
if mod(num1, 100)==0
if mod(num1, 400)==0
disp([num2str(num1),' is a leap year'])
else
disp([num2str(num1),' is not a leap year'])
end
else
disp([num2str(num1),' is a leap year'])
end
else
disp([num2str(num1),' is not a leap year'])
end
Output:
Enter an year:2000
2000 is a leap year
Enter an year:1800
1800 is not a leap year
Enter an year:1804
1804 is a leap year
Program 7
% 8. Write a MATLAB program to
% input any alphabet and check
% whether it is vowel or consonant.
clc
close all
clear all
% chr = 'c';
chr = input('Enter a character: ','s');
logicalVal = (chr == 'a' || chr == 'e' || chr == 'i' || chr == 'o'
|| chr == 'u');
% Both uppercase and lower case letters are checked in matlab
MALAB Tutorials 02 +91 8095558063
[email protected],
[email protected] www.bkpacademy.com
MATLAB Bal Kishor Premier Academy BASIC MATLAB
Program
if logicalVal
fprintf('%s is vowel\n', chr);
else
fprintf('%s is consonant\n', chr);
end
Output:
Enter a character: s
s is consonant
Enter a character: e
e is vowel
Enter a character: E
E is consonant
Program 8
% 9. Write a MATLAB program to
% input week number (1-7) and print
% week day (Sunday-Saturday).
clc
close all
clear all
num1 = input('Enter week number 1-7:');
switch num1
case 1
fprintf('Sunday\n');
case 2
fprintf('Monday\n');
case 3
fprintf('Tuesday\n');
case 4
fprintf('Wednesday\n');
case 5
fprintf('Thursday\n');
case 6
fprintf('Firday\n');
case 7
fprintf('Satruday\n');
otherwise
fprintf('Unexpected week number entered by user.\n')
end
Output:
Enter week number 1-7:3
Tuesday
MALAB Tutorials 02 +91 8095558063
[email protected],
[email protected] www.bkpacademy.com
MATLAB Bal Kishor Premier Academy BASIC MATLAB
Program
Enter week number 1-7:8
Unexpected week number entered by user.
Program 9
% 10. Write a MATLAB program to
% input month number and print
% number of days in that month.
clc
close all
clear all
num1 = input('Enter month number 1-12:');
% Method 1
switch num1
case 1
fprintf('It contains 31 days.\n');
case 2
fprintf('It contains 28 or 29 days.\n');
case 3
fprintf('It contains 31 days.\n');
case 4
fprintf('It contains 30 days.\n');
case 5
fprintf('It contains 31 days.\n');
case 6
fprintf('It contains 30 days.\n');
case 7
fprintf('It contains 31 days.\n');
case 8
fprintf('It contains 31 days.\n');
case 9
fprintf('It contains 30 days.\n');
case 10
fprintf('It contains 31 days.\n');
case 11
fprintf('It contains 30 days.\n');
case 12
fprintf('It contains 31 days.\n');
otherwise
fprintf('Unexpected month number entered by user.\n')
end
%% Method II
% p = num1==1 || num1==3|| num1==5|| num1==7|| num1==8|| num1==10||
num1==12;
% q = num1==2;
% if p
% fprintf('It contains 31 days.\n');
% elseif q
MALAB Tutorials 02 +91 8095558063
[email protected],
[email protected] www.bkpacademy.com
MATLAB Bal Kishor Premier Academy BASIC MATLAB
Program
% fprintf('It contains 28 or 29 days.\n');
% else
% fprintf('It contains 30 days.\n');
% end
Output:
Enter month number 1-12:5
It contains 31 days.
Enter month number 1-12:2
It contains 28 or 29 days.
See you in next tutorial…!!!
Thanks
𝐁𝐀𝐋 𝐊𝐈𝐒𝐇𝐎𝐑 𝐏𝐑𝐄𝐌𝐈𝐄𝐑 𝐀𝐂𝐀𝐃𝐄𝐌𝐘
Join us on our Facebook group: 𝑩𝑲𝑷 𝑨𝑪𝑨𝑫𝑬𝑴𝒀 𝑴𝑨𝑻𝑳𝑨𝑩
MALAB Tutorials 02 +91 8095558063
[email protected],
[email protected] www.bkpacademy.com