Accept 2 numbers from user

4 visualizzazioni (ultimi 30 giorni)
Josaiah Ang
Josaiah Ang il 27 Mag 2015
Commentato: Walter Roberson il 27 Mag 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

Risposte (1)

Andrei Bobrov
Andrei Bobrov il 27 Mag 2015
Modificato: Andrei Bobrov il 27 Mag 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 Commenti
Josaiah Ang
Josaiah Ang il 27 Mag 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 il 27 Mag 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.

Accedi per commentare.

Categorie

Scopri di più su Variables in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by