Accept 2 numbers from user
18 次查看(过去 30 天)
显示 更早的评论
This is my current code. how do i add this part of my algorithm into matlab code
integer :: a, b, c
integer :: abc, a3b3c3
integer :: count
count = 0
count = count + 1
clc
num1 = input('Enter num1 value: ');
num2= input('Enter num2 value: ');
fprintf('Armstrong numbers between %d and %d are: \n',num1 ,num2);
for a = 1 : 9
for b = 0 : 9
for c = 0 : 9
n = a*100 + b*10 + c;
an = a^3 + b^3 + c^3;
if n == an
fprintf('%d, ',an)
end
end
end
end
0 个评论
回答(1 个)
Andrei Bobrov
2015-5-27
编辑:Andrei Bobrov
2015-5-27
a1 = 1 : 9;
b1 = 0 : 9;
c1 = 0 : 9;
num1 = input('Enter num1 value: ');
num2= input('Enter num2 value: ');
[c,b,a] = ndgrid(c1,b1,a1);
n = a*100 + b*10 + c;
an = a^3 + b^3 + c^3;
t1 = n >= num1 & n <= num2;
t2 = an == n;
out = an(t1 & t2);
3 个评论
Walter Roberson
2015-5-27
The calculation for Armstrong numbers depends upon the number of digits. The program is for 3 digit Armstrong numbers only. You are trying to find some 1 digit Armstrong numbers and some 2 digit Armstrong numbers. Your algorithm needs to be changed for that.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Particle Swarm 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!