Accept 2 numbers from user

4 ビュー (過去 30 日間)
Josaiah Ang
Josaiah Ang 2015 年 5 月 27 日
コメント済み: Walter Roberson 2015 年 5 月 27 日
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

回答 (1 件)

Andrei Bobrov
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 件のコメント
Josaiah Ang
Josaiah Ang 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);
i'm getting the output like this. it not displaying any result.
Walter Roberson
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 ExchangeVariables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by