0% found this document useful (0 votes)
118 views

Introduction To MATLAB: Finding Factorial Code

MATLAB Codes for various small programs Like code for factorial, multiplication without inbuilt functions, checking palindrome, also many other smaller codes.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views

Introduction To MATLAB: Finding Factorial Code

MATLAB Codes for various small programs Like code for factorial, multiplication without inbuilt functions, checking palindrome, also many other smaller codes.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Introduction to MATLAB

Question 1:
Finding Factorial Code:
clear;clc; p=input('Enter a number \n'); i=p; if(~isfloat(int8(p))&&p~=0) while(i~=1 && p~=0) p=p*(i-1); i=i-1; end disp('The factorial of the number is ');p end if(p==0) disp(' Factorial of zero is zero, enter a non zero integer '); end if(isfloat(p)) disp(' Enter a integer number ') break; end

Output:

Question 2:
Fibonocci Series: Code:
clear;clc; a(1,1)=0; a(1,2)=1; i=3;y=0; n=input('Enter a number \n'); if(n>=2) while(y<n) y=a(1,i-1)+a(1,i-2); if(y<n) a(1,i)=y; else break; end i=i+1; end disp('Fibonocci series of given number is');

a else a end

Output:

Question 3:
Simple Calculator Code:
clear;clc; a=input('\n Enter first number \n'); b=input('\n Enter second number \n'); disp('1) Addition'); disp('2) Subtraction'); disp('3) Division'); disp('4) Multiplication');

i= input('\n Enter your choice \n'); if(i==1) add(a,b); elseif(i==2) sub(a,b); elseif(i==3) div(a,b); elseif(i==4) mul(a,b); else disp('Option not defined'); break; end

Functions:
function sum= add(a,b) disp('Your choice is addition'); disp('Sum of the numbers is'); sum= a+b return; end

function diff=sub(a,b) disp('Your choice is subtraction'); disp('Difference of the two numbers is'); diff=a-b return; end

function quotient=div(a,b) if(b==0) disp('Division by zero not defined, enter another number'); return; else disp('Your choice is division'); disp('Quotient after division'); quotient=a/b return; end end

function product=mul(a,b) disp('Your choice is multiplication'); disp('Product of the numbers'); product=a*b return; end

Output:

Question 4:
Palindrome Checker Code:
clear;clc; a=input('Enter string \n','s'); f=length(a); for i=1:f/2 if(a(i)==a(f-i+1)) count=1; else count=0; break; end end if(count) disp('Given string is a palindrome'); else disp('Given string is not a palindrome'); end

Output:

Question 5:
clear;clc; disp('Perform addition, subtraction, multiplication of matrices'); m=input('\n Enter number of rows of first matrix \n'); n=input('\n Enter number of coloumns of first matrix \n'); p=input('\n Enter number of rows of second matrix \n'); q=input('\n Enter number of coloumns of second matrix \n'); disp('Enter first matrix'); for g=1:m for h=1:n a(g,h)=input('Enter element of first matrix '); end end for g=1:p for h=1:n b(g,h)=input('Enter element of second matrix '); end end a b disp('1)Addition of matrices');disp('2)Subtraction of matrices');disp('3)Multiplication of matrices'); i=input('\n Enter your choice \n'); if(i==1) matrix_add(a,b,m,n,p,q); elseif(i==2) matrix_sub(a,b,m,n,p,q); elseif(i==3) matrix_mul(a,b,m,n,p,q); else disp('Incorrect choice');break; end

Functions:
function c=matrix_add(a,b,m,n,p,q); if(m==p&&n==q) for g=1:m for h=1:n c(g,h)=a(g,h)+b(g,h); end end c else disp('Matrix dimentions does not match, Addition cannot be performed'); return; end end

function c=matrix_sub(a,b,m,n,p,q); if(m==p&&n==q)

for g=1:m for h=1:n c(g,h)=a(g,h)-b(g,h); end end c else disp('Matrix dimentions does not match, Subtraction cannot be performed'); return; end end

function c=matrix_mul(a,b,m,n,p,q); if(n==p) c=zeros(m,q); for g=1:m for h=1:q for x=1:p c(g,h)=c(g,h)+a(g,x)*b(x,h); end end end c else disp('Matrix dimentions does not match, Multiplication cannot be performed'); return; end end

Output:

Question 6:
Proper Divisors Code:
clear;clc; num=input('Enter a number \n'); for i=1:num/2; if(~mod(num,i)) disp('Proper divisor'); i else i=i+1; end end

Output:

Question 7:
Even numbers between 21 and 65 Code:
clear;clc; count=1; a=zeros(1,22); for i=21:65 if(~mod(i,2)) a(count)=i; count=count+1; else i=i+1; end end disp('Even numbers between 21 and 65'); a

Output:

Question 8:
Matrix operations Code:
clear;clc; a=[4 5 2 6]; disp('1) Add 16'); disp('2)Add 3 to ODD indexed numbers'); disp('3)Create another vector with elements as square root');disp('4)Create another vector with elements as square elements'); i=input('Enter choice \n'); if(i==1) for j=1:4 a(j)=a(j)+16; end elseif(i==2) for j=1:4 if(mod(j,2)) a(j)=a(j)+3; end end elseif(i==3) for j=1:4 a(j)=sqrt(a(j)); end

elseif(i==4) for j=1:4 a(j)=a(j)^2; end else disp('Enter valid choice'); break; end a

Output:

Question 9:
clear;clc; a=[2 7 9 7;3 1 5 6;8 1 2 5]; disp('1)Assign even number rows of A to another matrix'); disp('2)Assign odd number rows of A to another matrix'); disp('3)Assign transpose of A to another matrix'); disp('4)Multiply A to its transpose and store it in another matrix'); i=input('\n Enter your choice \n'); if(i==1) assign_even_rows(a); elseif(i==2) assign_odd_rows(a); elseif(i==3) find_transpose(a); elseif(i==4) multiply_to_transpose(a); else disp('You have given a choice out of scope'); break; end

Functions:
function b=assign_even_rows(a) f=size(a); k=1; for m=1:f(1) if(~mod(m,2)) for n=1:f(2) b(k,n)=a(m,n); end k = k+1; end end b end

function c=assign_odd_rows(a) f=size(a); k=1; for m=1:f(1) if(mod(m,2)) for n=1:f(2) c(k,n)=a(m,n); end k = k+1; end end c end

function d=find_transpose(a) f=size(a); for m=1:f(1) for n=1:f(2) d(n,m)=a(m,n); end

end d return; end

function e=multiply_to_transpose(a) f=size(a); if(f(1)~=f(2)) disp('Input should be square matrix'); return; end tr=find_transpose(a); e=zeros(f(1),f(1)); for m=1:f(1) for n=1:f(1)

for x=1:f(1) e(m,n)=e(m,n)+tr(m,n)*a(n,m); end end end e end

Output:

Question 10:
clear;clc; for i=1:3 n=input('Enter value of n \n'); if(n>1) m=n+1 else m=n-1 end end

Output:

Question 11:
Code:
clear;clc; for i=1:4 x=input('Enter the value of X \n'); if((x>0)&&(x<10)) y=4*x elseif((x>10)&&(x<40)) y=10*x else y=500 end end

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy