Accept 2 numbers from user

4 views (last 30 days)
Josaiah Ang
Josaiah Ang on 27 May 2015
Commented: Walter Roberson on 27 May 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

Answers (1)

Andrei Bobrov
Andrei Bobrov on 27 May 2015
Edited: Andrei Bobrov on 27 May 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 Comments
Josaiah Ang
Josaiah Ang on 27 May 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 on 27 May 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.

Sign in to comment.

Categories

Find more on Variables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!