0% found this document useful (0 votes)
2 views16 pages

MATLAB Programming Assignment Solutions

The document contains a series of MATLAB programs that demonstrate various programming concepts such as temperature conversion, number classification (even/odd, positive/negative), finding the largest number, factorial calculation, summation, and generating Fibonacci sequences. Each program includes user input prompts and displays the corresponding outputs. The examples illustrate basic control structures like if-else statements, loops, and functions.

Uploaded by

manukolhe233
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)
2 views16 pages

MATLAB Programming Assignment Solutions

The document contains a series of MATLAB programs that demonstrate various programming concepts such as temperature conversion, number classification (even/odd, positive/negative), finding the largest number, factorial calculation, summation, and generating Fibonacci sequences. Each program includes user input prompts and displays the corresponding outputs. The examples illustrate basic control structures like if-else statements, loops, and functions.

Uploaded by

manukolhe233
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

MATLAB Assignment No.

2
Programs and Outputs
>> C = input('Enter temperature in Celsius: ');
>> F = (9/5)*C + 32
Enter temperature in Celsius: 25
F = 77
>> F = input('Enter temperature in Fahrenheit: ');
>> C = (5/9)*(F - 32)
Enter temperature in Fahrenheit: 98.6
C = 37
>> n = input('Enter a number: ');
>> if mod(n,2)==0
disp('Even number')
else
disp('Odd number')
end
Enter a number: 7
Odd number
>> n = input('Enter a number: ');
>> if n > 0
disp('Positive')
elseif n < 0
disp('Negative')
else
disp('Zero')
end
Enter a number: -5
Negative
>> a = input('Enter first number: ');
>> b = input('Enter second number: ');
>> c = input('Enter third number: ');
>> if a>b
if a>c
largest=a;
else
largest=c;
end
else
if b>c
largest=b;
else
largest=c;
end
end
>> fprintf('Largest number = %d\n', largest);
Enter first number: 12
Enter second number: 8
Enter third number: 15
Largest number = 15
>> n = input('Enter a number: ');
>> for i = 1:10
fprintf('%d x %d = %d\n', n, i, n*i);
end
Enter a number: 5
5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50
>> n = input('Enter a number: ');
>> fact = 1; for i = 1:n fact = fact * i; end
>> fprintf('Factorial = %d\n', fact);
Enter a number: 5
Factorial = 120
>> N = input('Enter value of N: ');
>> sumN = 0; for i = 1:N sumN = sumN + i; end
>> fprintf('Sum = %d\n', sumN);
Enter value of N: 10
Sum = 55
>> N = input('Enter value of N: ');
>> i = 1; while i <= N disp(i); i = i + 1; end
Enter value of N: 5
1
2
3
4
5
>> n = input('Enter a number: ');
>> rev = 0; while n > 0 r = mod(n,10); rev = rev*10 + r; n = floor(n/10); end
>> fprintf('Reverse = %d\n', rev);
Enter a number: 1234
Reverse = 4321
>> n = input('Enter a number: ');
>> temp = n; rev = 0; while n > 0 r = mod(n,10); rev = rev*10 + r; n = floor(n
>> if temp == rev
disp('Palindrome number')
else
disp('Not a palindrome')
end
Enter a number: 121
Palindrome number
>> year = input('Enter year: ');
>> if mod(year,400)==0 || (mod(year,4)==0 && mod(year,100)~=0)
disp('Leap Year')
else
disp('Not a Leap Year')
end
Enter year: 2024
Leap Year
>> P = input('Enter Principal: ');
>> R = input('Enter Rate: ');
>> T = input('Enter Time (years): ');
>> SI = (P*R*T)/100;
>> fprintf('Simple Interest = %.2f\n', SI);
Enter Principal: 1000
Enter Rate: 5
Enter Time (years): 2
Simple Interest = 100.00
>> N = input('Enter value of N: ');
>> sumEven = 0; for i = 1:N if mod(i,2)==0 sumEven = sumEven + i; end end
>> fprintf('Sum of even numbers = %d\n', sumEven);
Enter value of N: 10
Sum of even numbers = 30
>> N = input('Enter number of terms: ');
>> a = 0; b = 1; fprintf('%d %d ', a, b); count = 2; while count < N
c = a + b; fprintf('%d ', c); a = b; b = c; count = count + 1; end
Enter number of terms: 7
0 1 1 2 3 5 8

You might also like