Accept 2 numbers from user

4 visualizaciones (últimos 30 días)
Josaiah Ang
Josaiah Ang el 27 de Mayo de 2015
Comentada: Walter Roberson el 27 de Mayo de 2015
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

Respuestas (1)

Andrei Bobrov
Andrei Bobrov el 27 de Mayo de 2015
Editada: Andrei Bobrov el 27 de Mayo de 2015
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 comentarios
Josaiah Ang
Josaiah Ang el 27 de Mayo de 2015
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);
i'm getting the output like this. it not displaying any result.
Walter Roberson
Walter Roberson el 27 de Mayo de 2015
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Variables en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by