1. Write a matlab program that takes a number from the user as an input and outp
ID: 3564669 • Letter: 1
Question
1. Write a matlab program that takes a number from the user as an input and outputs whether the number is
perfect or not.
Example run:
Input number: 5
Not a perfect number!
Input number: 28
Perfect number!
2. Write a matlab program to generate a list of perfect numbers!. (at least up to 6)
Example run:
How many perfect numbers do you wish for? 4
6 28 496 8128
I feel as if I am overthinking this problem. If someone can help me out and explain how to solve this it will be greatly appreciated.
If you don't know a perfect number is a number whose divisors all add up to equal that number.
For example: 6
6 can be divided by 1, 2, and 3.
1+2+3=6
6 is a perfect number.
The number 8 is not a perfect number because 1,2, and 4 do not add up to 8.
Thanks you guys
Explanation / Answer
% 1. Write a matlab program that takes a number from the user as an input and outputs whether the number is
% perfect or not.
k = input('Input number: ');
sum = 0;
for i=1:k-1
if mod(k,i)==0
sum = sum + i;
end
end
if sum != k
fprintf(' Not a perfect number!')
else
fprintf(' Perfect number!')
end
% 2. Write a matlab program to generate a list of perfect numbers!. (at least up to 6)
k = input('How many perfect numbers do you wish for? ');
count = 0;
p = 2;
fprintf(' ');
while count < k
sum = 0;
for i=1:p-1
if mod(p,i)==0
sum = sum + i;
end
end
if sum == p
fprintf('%d ',p);
count++;
end
p++;
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.