0% found this document useful (0 votes)
6 views2 pages

Atik

Uploaded by

tusarrahamanaiub
Copyright
© © All Rights Reserved
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)
6 views2 pages

Atik

Uploaded by

tusarrahamanaiub
Copyright
© © All Rights Reserved
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/ 2

%Question 1

function gcdlcm(a,b)
gcd_result = gcd(a,b);
lcm_result = lcm(a,b);
fprintf('GCD of %d and %d is: %d\n', a, b, gcd_result);
fprintf('LCM of %d and %d is: %d\n', a, b, lcm_result);
end
gcdlcm(11,20)

GCD of 11 and 20 is: 1


LCM of 11 and 20 is: 220

%Question 2
function checkTriangle(a,b,c)
side1 = a;
side2 = b;
side3 = c;
if (side1 + side2 > side3) && (side2 + side3 > side1) && (side1 + side3
> side2)
fprintf('These sides can form a triangle.\n');

sides = sort([side1, side2, side3]);

if abs(sides(1)^2 + sides(2)^2 - sides(3)^2) < 1e-10


fprintf('This is a right-angled triangle.\n');
else
fprintf('This is not a right-angled triangle.\n');
end
else
fprintf('These sides cannot form a triangle.\n');
end
end
checkTriangle(1,3,5)

These sides cannot form a triangle.

%Question 3
function calculatePopulation()
current_pop = 10000;
growth_rate = 0.10;

fprintf('\nPopulation for the past 10 years:\n');


for year = -10:-1
past_pop = current_pop / (1 + growth_rate)^abs(year);
fprintf('Year %d: %.0f\n', year, past_pop);
end

fprintf('\nCurrent population (Year 0): %.0f\n', current_pop);

1
fprintf('\nProjected population for the next 10 years:\n');
for year = 1:10
future_pop = current_pop * (1 + growth_rate)^year;
fprintf('Year %d: %.0f\n', year, future_pop);
end
end
calculatePopulation()

Population for the past 10 years:


Year -10: 3855
Year -9: 4241
Year -8: 4665
Year -7: 5132
Year -6: 5645
Year -5: 6209
Year -4: 6830
Year -3: 7513
Year -2: 8264
Year -1: 9091

Current population (Year 0): 10000

Projected population for the next 10 years:


Year 1: 11000
Year 2: 12100
Year 3: 13310
Year 4: 14641
Year 5: 16105
Year 6: 17716
Year 7: 19487
Year 8: 21436
Year 9: 23579
Year 10: 25937

% Question 4
function fibonacci(n)
a = 0;
b = 1;

fprintf('Fibonacci sequence up to %d:\n', n);


fprintf('%d ', a);

while b <= n
fprintf('%d ', b);
temp = a + b;
a = b;
b = temp;
end
fprintf('\n');
end
fibonacci(150)

Fibonacci sequence up to 150:


0 1 1 2 3 5 8 13 21 34 55 89 144

You might also like