BIL113 E LAB6 HW3 Solutions PDF

Title BIL113 E LAB6 HW3 Solutions
Author Eray Senses
Course Matlab
Institution Istanbul Teknik Üniversitesi
Pages 2
File Size 61.8 KB
File Type PDF
Total Downloads 46
Total Views 148

Summary

functions at matlab...


Description

BIL113E – Introduction to Scientific and Engineering Computing (MATLAB) HOMEWORK #3 Due Date: April 9, 2020 1.

Write a program in MATLAB to display the following pattern using nested loops?

***** **** *** ** * for i=5:-1:1 for j=1:i fprintf('*'); end fprintf('\n'); end 2. In number theory, a semi-perfect number is a positive integer number that is equal to the sum of its largest three positive divisors, excluding the number itself. For example; 18 is a semiperfect number, the positive divisors of this number are 1,2,3,6,9,18, and the sum of the three largest integer divisors (excluding the number itself) is 3 + 6 + 9 = 18. Write a MATLAB program that checks whether a given number is semi-perfect number. n=input('Enter a number '); a=0; %a is number of positive divisors sum=0; for i=n-1:-1:1 if mod (n,i)==0 sum=sum+i; a=a+1; end if a==3 break end end if a==3 && sum==n fprintf('%d is a semi-perfect number\n',n) else fprintf('%d is not a semi-perfect number\n',n) end

>> sm Enter a number 6 6 is a semi-perfect number >> sm Enter a number 12 12 is not a semi-perfect number

Write a MATLAB program that calculates the sum of the largest positive divisor 3. (excluding itself) and the smallest positive divisor (excluding 1) of a given number? n=input('Enter a number '); sum=0; for i=n-1:-1:1 if mod (n,i)==0 sum=sum+i;% i is largest positive divisor (excluding itself) break end end for j=2:n if mod(n,j)==0 sum=sum+j;%j is smallest positive divisor (excluding 1) break end end fprintf('sum= %d\n',sum);

>> aaa Enter a number 12 sum= 8...


Similar Free PDFs