0% found this document useful (0 votes)
18 views3 pages

Prac 5 Code

The document contains 5 programs demonstrating mathematical concepts and functions in MATLAB including division algorithm, prime numbers, GCD, Euclidean algorithm, and solving quadratic equations. Program 1 shows division algorithm, program 2 displays prime numbers and factors, program 3 finds GCD of number sets, program 4 creates a function for the Euclidean algorithm, and program 5 uses poly and root functions to find roots of a quadratic equation.

Uploaded by

Manav Parmar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Prac 5 Code

The document contains 5 programs demonstrating mathematical concepts and functions in MATLAB including division algorithm, prime numbers, GCD, Euclidean algorithm, and solving quadratic equations. Program 1 shows division algorithm, program 2 displays prime numbers and factors, program 3 finds GCD of number sets, program 4 creates a function for the Euclidean algorithm, and program 5 uses poly and root functions to find roots of a quadratic equation.

Uploaded by

Manav Parmar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

practical number 5

AIM : Algorithm and function

Program1 : write a program for division algorithm.


code :
disp('Division algorithm')
a=4461 //dividend
b=16//divisor
r=modulo(a,b) //remainder
k=fix(a/b)//quotient
j=b*k+r //dividend=divisor*quotient+remainder

disp(a,'value of a(given number)')


disp(j,'value of j(calculated with the formula)')

disp('the value of a and j are same. Hence division algorithm is true')


disp('Division algorithm')
-----------------------------------------------------------------------------------
-----------------------
output:
4461
value of a(given number)
4461
value of j(calculated with the formula)
'the value of a and j are same. Hence division algorithm is true')
-----------------------------------------------------------------------------------
-------------------------
program2: Display prime numbers less than 50 and find factors of 21,24 and 1729
code:
x=50;
disp('primes number less than 50')
y=primes(x)
disp(y)
disp('factor of 21, 24, and 1729')
x=factor(21)
y=factor(24)
z=factor(1729)
disp(x)
disp(y)
disp(z)
-----------------------------------------------------------------------------------
---------------------
output :
"primes number less than 50"
y =
column 1 to 9
2. 3. 5. 7. 11. 13. 17. 19. 23.
column 10 to 15
29. 31. 37. 41. 43. 47.

"factor of 21, 24, and 1729"


ans =
3. 7.
ans =
2. 2. 2. 3.
ans =
7. 13. 19.
-----------------------------------------------------------------------------------
-------------------------
program3: Find GCD of following numbers:
(12,22,24)
(15,32,44)
(13,26,39,45)
(12,24,48,34,35)
code:
disp("the GCD of following numbers")
v=int([12,22,24]) // numbers are 12,22,24
disp(gcd(v)) // use of GCD for showing GCD
v1=int([15,32,44])
disp(gcd(v1))
v2=int([13,26,39,45])
disp(gcd(v2))
v3=int([12,24,48,34,35])
disp(gcd(v3))
-----------------------------------------------------------------------------------
-------------
output:
"the GCD of following numbers"
v =
12. 22. 24.

2.
v1 =
15. 32. 44.

1.
v2 =
13. 26. 39. 45.

1.
v3 =
12. 24. 48. 34. 35.

1.
-----------------------------------------------------------------------------------
---------------------------------
program4: Create a function for use of Euclidean algorithm
code :
function [ans]=myf(a,b) //myf is name of your function
q=fix(a/b); //quotient
rem=modulo(a,b); // reminder
k=q*b+rem; // division formula
ans=b
if(rem~=0)then // check value of rem is not equal to 0
myf(b,rem) //call function again and replace of values a by b and b by rem
end
endfunction
ans=myf(330,156)
disp(ans,'GCD of given numbers')
-----------------------------------------------------------------------------------
-----------------------------------
output :
6
GCD of given numbers
-----------------------------------------------------------------------------------
---------------------------------
program5: Write a program for roots of equation using poly and root function
code:
disp('factor of equations')
t=poly([2,3],'t') //we are passing factor then equation formed
a=factors(t) // we will get factors of the eqn
disp(a)

t=poly(0,'t')
f=t^2-3*t+2
r=int32(roots(f))// roots of equation t^2-3t+2
disp(r)// roots are 1 and 2
-----------------------------------------------------------------------------------
----------
output :
"factor of equations"
t =
6 -5t +t²
a =

a(1)

-3 +t

a(2)

-2 +t

r =
2
1

You might also like