Search This Blog

Tuesday, September 13, 2016

To accept two numbers from the user and display perfect numbers between these two numbers


Question:

To accept two numbers from the user and display perfect numbers between these two numbers


clc
num1 =input(' Enter your 1st number: ');
num2 =input('\n Enter your 2st number: ');
output = zeros(1,num2);
for i = num1:num2
if true
end

test = 1:i-1;
if (sum(test(mod(i,test) == 0)) == i)
output(i) = i;
end
end

output(output == 0) = []

Output: I just write in command window

       
                  Enter your 1st number : 1
                      Enter your 2st number : 100


       output =

                      6    28




1 comment:

  1. Hi, can you explain to me how it works?
    output = zeros(1,num2);
    for i = num1:num2
    if true
    end

    test = 1:i-1;
    if (sum(test(mod(i,test) == 0)) == i)
    output(i) = i;
    end
    end

    output(output == 0) = []

    ReplyDelete