Write a Matlab code to check. .. whether a given number is an amstrong number using functions.

10 ビュー (過去 30 日間)
Siddharth Pratyush
Siddharth Pratyush 2022 年 2 月 1 日
回答済み: Stephen23 2022 年 2 月 1 日
need code

回答 (2 件)

Murugan C
Murugan C 2022 年 2 月 1 日
function check_amstrong_number(input)
if isnumeric(input)
input_ams = num2str(input);
else
error('Input shoud be numberic')
end
amst_num = 0;
for i1 = 1 : length(input_ams)
amst_num = amst_num + str2double(input_ams(i1)) ^ 3;
end
if amst_num == input
disp(['Given Number ' input_ams ' is Amstrong_Number'])
else
disp(['Given Number ' input_ams ' is NOT a Amstrong_Number'])
end

Stephen23
Stephen23 2022 年 2 月 1 日
N = 153;
S = num2str(N);
Z = N==sum((S-'0').^numel(S))
Z = logical
1

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by