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

lab exam

Uploaded by

P.tejasmani
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)
1 views

lab exam

Uploaded by

P.tejasmani
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/ 7

1.

Matlab

Factorial of a Number using a Loop:

matlab

Copy code

% Input

n = input('Enter a number: ');

factorial_result = 1;

% Loop to calculate factorial

for i = 1:n

factorial_result = factorial_result * i;

end

% Output

fprintf('The factorial of %d

4o mini

You said:

give everthing as short as possible

ChatGPT said:

ChatGPT

Here are the shortest possible versions of the MATLAB solutions for each task:

1. Factorial using a Loop:

matlab

Copy code

n = input('Enter number: ');

f = 1; for i = 1:n, f = f*i; end

disp(f);

2. Prime Check Function:

matlab

Copy code

function p = isPrime(n)
p = all(mod(n, 2:n-1) > 0);

end

3. Sum of Matrix Elements:

matlab

Copy code

A = [1 2 3; 4 5 6];

sum_val = sum(A(:));

disp(sum_val);

4. Fibonacci Series:

matlab

Copy code

n = input('Enter n: ');

f = [0 1]; for i = 3:n, f(i) = f(i-1) + f(i-2); end

disp(f);

5. Bisection Method for Root Finding:

matlab

Copy code

f = @(x) x^2 - 4; % Function

a = 0; b = 3; tol = 1e-6;

while (b-a)/2 > tol

c = (a+b)/2;

if f(c) == 0, break; end

if f(c)*f(a) < 0, b = c; else a = c; end

end

disp(c);

6. Palindrome Check:

matlab

Copy code

str = input('Enter string: ', 's');

if strcmp(str, flip(str)), disp('Palindrome'); else disp('Not Palindrome'); end

7. Roots of a Polynomial:
matlab

Copy code

p = [1 -6 11 -6];

r = roots(p);

disp(r);

8. Simpson's Rule for Integration:

matlab

Copy code

f = @(x) x.^2;

a = 0; b = 2; n = 4; h = (b-a)/n;

x = a:h:b; y = f(x);

I = (h/3) * (y(1) + 4*sum(y(2:2:end)) + 2*sum(y(3:2:end-1)) + y(end));

disp(I);

9. Eigenvalues and Eigenvectors:

matlab

Copy code

A = [4 1; 2 3];

[eigVecs, eigVals] = eig(A);

disp(eigVecs);

disp(diag(eigVals));

10. Expression Evaluation:

matlab

Copy code

a = 3; b = 9;

c = 2*a + b^2 - a*b + b/a - 10;

disp(c);

11. Matrix Multiplication (Associativity):

matlab

Copy code

A = [1 2; 3 4]; B = [5 6; 7 8]; C = [9 10; 11 12];

disp(A*(B*C) == (A*B)*C);
12. Plot Sine and Cosine:

matlab

Copy code

x = 0:0.1:2*pi;

plot(x, sin(x), x, cos(x)); legend('sin(x)', 'cos(x)');

13. Derivative of f(x) = sin(x) + cos(x):

matlab

Copy code

f = @(x) sin(x) + cos(x);

f_prime = @(x) cos(x) - sin(x);

fplot(f_prime, [0 2*pi]);

14. 3D Surface Plot of z = x^2 - y^2:

matlab

Copy code

[x, y] = meshgrid(-3:0.1:3, -3:0.1:3);

z = x.^2 - y.^2;

surf(x, y, z);

15. Plot Two Functions with Labels:

matlab

Copy code

x = 0:0.1:2*pi;

y1 = 2*sin(x); y2 = cos(cos(x));

plot(x, y1, x, y2);

legend('2sin(x)', 'cos(cos(x))');

16. Scatter Plot with Labels:

matlab

Copy code

x = rand(1, 10); y = rand(1, 10);

scatter(x, y);

xlabel('X-axis'); ylabel('Y-axis');
1. Arithmetic Operations:

python

Copy code

a, b = 10, 5

print("Addition:", a+b, "Subtraction:", a-b, "Multiplication:", a*b, "Division:", a/b)

2. Print Pattern:

python

Copy code

for i in range(1, 6): print(''.join(map(str, range(1, i+1))))

3. Convert Temperature (Celsius to Fahrenheit):

temp = float(input("Enter temperature in Celsius: "))

fahrenheit = (temp * 9 / 5) + 32

print(f"Temperature in Fahrenheit: {fahrenheit:.2f}")

4. Minimum Coins for Exact Amount:

X = int(input("Enter amount: "))

if X % 5 != 0: print(-1)

else: print(X // 10 + (X % 10) // 5)

5. Construct Star Pattern:

python

Copy code

for i in range(1, 6): print('*' * i)

for i in range(4, 0, -1): print('*' * i)

6. Factorial using Recursion:

python

Copy code

def factorial(n): return 1 if n == 0 else n * factorial(n-1)

print(factorial(int(input("Enter number: "))))

7. Find Largest of Three Numbers:

python
Copy code

a, b, c = 3, 5, 7

print(max(a, b, c))

8. Check Odd or Even:

python

Copy code

num = int(input("Enter number: "))

print("Even" if num % 2 == 0 else "Odd")

9. Factorial of Numbers Between a and b:

python

Copy code

a, b = 3, 6

for num in range(a, b+1):

fact = 1

for i in range(1, num+1): fact *= i

print(f"Factorial of {num}: {fact}")

10. Left Pascal Triangle Pattern:

python

Copy code

n = int(input("Enter number of rows: "))

for i in range(1, n+1): print(' '.join(map(str, range(1, i+1))))

for i in range(n-1, 0, -1): print(' '.join(map(str, range(1, i+1))))

11. Right-Angled Triangle with Stars:

python

Copy code

n = int(input("Enter number of rows: "))

for i in range(1, n+1): print('*' * i)

12. Sum of First 100 Odd Numbers:

python

Copy code

print(sum(range(1, 200, 2)))


13. Number Pattern:

python

Copy code

for i in range(5, 0, -1): print(str(i)*i)

14. Simple and Compound Interest:

python

Copy code

p, r, t = 1000, 5, 2

SI = (p * r * t) / 100

CI = p * ((1 + r / 100) ** t - 1)

print(f"SI: {SI}, CI: {CI}")

15. Pyramid Pattern:

python

Copy code

for i in range(1, 6): print('* ' * i)

16. Sum of Odd and Even Numbers:

python

Copy code

even_sum, odd_sum = 0, 0

for i in range(1, 11):

if i % 2 == 0: even_sum += i

else: odd_sum += i

print("Even sum:", even_sum, "Odd sum:", odd_sum)

These are the shortest versions of the programs for each task.

You might also like