0% found this document useful (0 votes)
13 views6 pages

50 MATLAB Loop Questions

Uploaded by

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

50 MATLAB Loop Questions

Uploaded by

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

50 MATLAB Loop 7. Sum of digits of 456.

Questions n = 456;
sum = 0;
while n > 0
1. Print numbers 1 to 10 using a for loop. sum = sum + mod(n,10);
n = floor(n/10);
for i = 1:10
end
disp(i)
disp(sum)
end

8. Squares of numbers 1 to 10.


2. Print numbers from 10 to 1 using a while loop.
for i = 1:10
i = 10;
fprintf('%d^2 = %d\n', i, i^2);
while i >= 1
end
disp(i)
i = i - 1;
end
9. Count odd numbers from 1 to 100.

3. Print even numbers from 2 to 20. count = 0;


for i = 1:2:100
for i = 2:2:20 count = count + 1;
disp(i) end
end disp(count)

4. Sum of first 100 natural numbers.


10. Reverse a vector.
sum = 0;
for i = 1:100 v = [5 4 3 2 1];
sum = sum + i; rev = [];
end for i = length(v):-1:1
disp(sum) rev(end+1) = v(i);
end
disp(rev)
5. Factorial of 5.

n = 5;
fact = 1; 11. Largest element in array.
for i = 1:n
fact = fact * i; arr = [3, 8, 1, 5];
end maxVal = arr(1);
disp(fact) for i = 2:length(arr)
if arr(i) > maxVal
maxVal = arr(i);
end
6. Multiplication table of 7. end
disp(maxVal)
for i = 1:10
fprintf('7 x %d = %d\n', i, 7*i);
end
12. Print characters of string.
str = 'MATLAB'; n = 10;
for i = 1:length(str) fib = zeros(1,n);
disp(str(i)) fib(1) = 0;
end fib(2) = 1;
for i = 3:n
fib(i) = fib(i-1) + fib(i-2);
end
13. Average of array. disp(fib)

arr = [2, 4, 6, 8];


sum = 0;
for i = 1:length(arr) 18. Count vowels in a string.
sum = sum + arr(i);
end str = 'MATLAB Programming';
avg = sum / length(arr); vowels = 'aeiouAEIOU';
disp(avg) count = 0;
for i = 1:length(str)
if contains(vowels, str(i))
count = count + 1;
14. While loop stops when sum exceeds 100. end
end
i = 1;
disp(count)
sum = 0;
while sum <= 100
sum = sum + i;
i = i + 1; 19. Rotate a matrix clockwise by 90 degrees.
end
disp(sum) A = [1 2; 3 4];
rotated = A';
rotated = rotated(:, end:-1:1);
disp(rotated)
15. Count positive numbers in array.

arr = [-1, 2, 5, -3, 4];


count = 0; 20. Find GCD of two numbers using loop.
for i = 1:length(arr)
if arr(i) > 0 a = 48; b = 18;
count = count + 1; while b ~= 0
end temp = b;
end b = mod(a, b);
disp(count) a = temp;
end
disp(a)

16. Print a 5x5 multiplication table using nested


loops.
21. Reverse a number using while loop.
for i = 1:5
for j = 1:5 n = 1234;
fprintf('%d\t', i*j); rev = 0;
end while n > 0
fprintf('\n'); rev = rev * 10 + mod(n, 10);
end n = floor(n / 10);
end
disp(rev)

17. Generate a Fibonacci series up to 10 terms.


n = 5;
for i = 0:n-1
22. Find all prime numbers between 1 and 50. for j = 0:i
fprintf('%d ', nchoosek(i,j));
for n = 2:50 end
isPrime = true; fprintf('\n');
for i = 2:sqrt(n) end
if mod(n, i) == 0
isPrime = false;
break;
end 26. Check if a number is a palindrome.
end
if isPrime n = 121;
disp(n) temp = n;
end rev = 0;
end while n > 0
rev = rev * 10 + mod(n, 10);
n = floor(n / 10);
end
23. Sum of all primes below 100. disp(rev == temp)

sum = 0;
for n = 2:99
isPrime = true; 27. Simulate dice roll 100 times and plot frequency.
for i = 2:sqrt(n)
if mod(n, i) == 0 rolls = randi([1 6], 1, 100);
isPrime = false; histogram(rolls, 6);
break; xlabel('Dice Face'); ylabel('Frequency');
end
end
if isPrime
sum = sum + n;
28. Count a specific number in a matrix.
end A = [1 2 3; 4 5 2; 2 6 7];
end count = sum(A(:) == 2);
disp(sum) disp(count)

24. Bubble sort using a loop. 29. Sum of diagonal elements of matrix.
arr = [5 2 9 1 5 6]; A = [1 2 3; 4 5 6; 7 8 9];
n = length(arr); sum_diag = 0;
for i = 1:n-1 for i = 1:min(size(A))
for j = 1:n-i sum_diag = sum_diag + A(i,i);
if arr(j) > arr(j+1) end
temp = arr(j); disp(sum_diag)
arr(j) = arr(j+1);
arr(j+1) = temp;
end
end 30. Print right-angled triangle of stars.
end
disp(arr) n = 5;
for i = 1:n
disp(repmat('*', 1, i))
end
25. Pascal’s triangle up to 5 rows.
31. Transpose a matrix using loop. 36. Matrix multiplication manually.

A = [1 2 3; 4 5 6]; A = [1 2; 3 4]; B = [5 6; 7 8];


[m,n] = size(A); C = zeros(2);
T = zeros(n,m); for i = 1:2
for i = 1:m for j = 1:2
for j = 1:n for k = 1:2
T(j,i) = A(i,j); C(i,j) = C(i,j) + A(i,k) *
end B(k,j);
end end
disp(T) end
end
disp(C)

32. Compound interest for 10 years.

P = 1000; r = 0.05; n = 10; 37. Create pattern:


for t = 1:n
1
A = P * (1 + r)^t;
12
fprintf('Year %d: %.2f\n', t, A);
123
End 1234

for i = 1:4
for j = 1:i
33. Evaluate e^x using series. fprintf('%d ', j);
end
x = 1; n = 10; fprintf('\n');
sum = 1; end
term = 1;
for i = 1:n
term = term * x / i;
sum = sum + term; 38. Insertion sort using for loop.
end
disp(sum) arr = [5 3 4 1 2];
for i = 2:length(arr)
key = arr(i);
j = i - 1;
34. Display Armstrong numbers from 100 to 999. while j >= 1 && arr(j) > key
arr(j+1) = arr(j);
for n = 100:999 j = j - 1;
d = num2str(n) - '0'; end
if sum(d.^3) == n arr(j+1) = key;
disp(n) end
end disp(arr)
end

39. Evaluate sin(x) using Taylor series.


35. Find LCM of two numbers.
x = pi/4; n = 5;
a = 15; b = 20; sum = 0;
gcd_ab = gcd(a,b); for i = 0:n
lcm_ab = (a*b)/gcd_ab; sum = sum + ((-1)^i * x^(2*i+1)) /
disp(lcm_ab) factorial(2*i+1);
end
disp(sum)
44. Remove all spaces from string.

40. Count duplicate values in array. str = 'Hello World MATLAB';


str(str == ' ') = [];
arr = [1 2 2 3 3 3]; disp(str)
u = unique(arr);
duplicates = 0;
for i = 1:length(u)
if sum(arr==u(i)) > 1 45. Draw 8x8 checkerboard pattern.
duplicates = duplicates + 1;
end A = zeros(8);
end for i = 1:8
disp(duplicates) for j = 1:8
A(i,j) = mod(i+j,2);
end
end
41. Monte Carlo simulation: estimate Pi. disp(A)

N = 10000; count = 0;
for i = 1:N
x = rand; y = rand; 46. Count frequency of words in string.
if x^2 + y^2 <= 1
count = count + 1; str = 'matlab is fun and matlab is
end powerful';
end words = split(str);
pi_estimate = 4 * count / N; uniqueWords = unique(words);
disp(pi_estimate) for i = 1:length(uniqueWords)
count = sum(strcmp(words,
uniqueWords{i}));
fprintf('%s: %d\n', uniqueWords{i},
42. Find all Pythagorean triplets < 100. count);
end
for a = 1:100
for b = a:100
c = sqrt(a^2 + b^2);
if mod(c,1)==0 && c<=100 47. 100 coin tosses and percentage of heads.
fprintf('%d %d %d\n', a, b,
c); tosses = randi([0 1], 1, 100);
end heads = sum(tosses);
end tails = 100 - heads;
end fprintf('Heads: %.2f%%\n', heads);
fprintf('Tails: %.2f%%\n', tails);

43. Gauss-Seidel method (1 iteration).


48. Quadratic equation for multiple inputs.
A = [4 -1 0; -1 4 -1; 0 -1 3];
b = [15; 10; 10]; a = [1 2]; b = [-3 -4]; c = [2 4];
x = zeros(3,1); for i = 1:length(a)
for i = 1:3 d = b(i)^2 - 4*a(i)*c(i);
x(i) = (b(i) - A(i,:)*x + A(i,i)*x(i)) root1 = (-b(i) + sqrt(d)) / (2*a(i));
/ A(i,i); root2 = (-b(i) - sqrt(d)) / (2*a(i));
end fprintf('Roots: %.2f, %.2f\n', root1,
disp(x) root2);
end
49. Approximate pi using Leibniz series.

n = 10000;
pi_approx = 0;
for k = 0:n
pi_approx = pi_approx +
((-1)^k)/(2*k+1);
end
pi_approx = 4 * pi_approx;
disp(pi_approx)

50. Euler’s method to solve dy/dx = x + y.

h = 0.1; x = 0:h:1; y = zeros(size(x));


y(1) = 1;
for i = 1:length(x)-1
y(i+1) = y(i) + h*(x(i) + y(i));
end
disp(y)

You might also like