code to accept 2 numbers and to display armstrong number between the 2?

17 ビュー (過去 30 日間)
bamdad goudarzi
bamdad goudarzi 2016 年 9 月 16 日
コメント済み: Kuldeep 2023 年 8 月 5 日
I'm new to mathlab and i have Absolutely no idea how to write this code. I don't want to use functions and prefer to use loops to solve this. But at this point anything goes.

採用された回答

KSSV
KSSV 2016 年 9 月 16 日
  3 件のコメント
KSSV
KSSV 2016 年 9 月 16 日
function val = ArmStrong(num1,num2)
val = [] ;
% Let i take numbers 100 to 999
for i = num1 : num2
% We examine every digit in i
is = num2str(i);
% i1 is the left-most digit in i
i1 = str2num(is(1));
% i2 is the middle digit
i2 = str2num(is(2));
% i3 is the right-most digit in i
i3 = str2num(is(3));
% We calculate the probable AN
an = i1^3 + i2^3 + i3^3;
% We compare to the number itself
if i == an
% We display the pair of equal numbers
val = [val ;[i an]] ;
end
end
end
Kuldeep
Kuldeep 2023 年 8 月 5 日
153

サインインしてコメントする。

その他の回答 (2 件)

Bhuvana Krishnaraj
Bhuvana Krishnaraj 2020 年 1 月 9 日
編集済み: Bhuvana Krishnaraj 2020 年 1 月 9 日
x=input('Enter number: ');
disp(x)
temp=x;
y = 0;
h=fprintf('%d',x); %% it return number of digits
while x > 0
t = mod(x,10);
y = y+t^h;
x = (x-t)/10;
end
if(y==temp)
disp('Amstrong');
else
disp('not amstrong');
end

Bhuvana Krishnaraj
Bhuvana Krishnaraj 2020 年 1 月 9 日
x=input('Enter number: ');
disp(x)
temp=x;
y = 0;
while x > 0
t = mod(x,10);
y = y+t^3;
x = (x-t)/10;
end
if(y==temp)
disp('Amstrong');
else
disp('not amstrong');
end

カテゴリ

Help Center および File ExchangeNumber games についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by