Impact-Site-Verification: dbe48ff9-4514-40fe-8cc0-70131430799e

Search This Blog

#Day80 #100DaysChallenge- Matlab Loops|Armstrong Number or not

#Day80-Armstrong Number or not

Task:

Write a code to given number is Armstrong or not 
371-Armstrong number
876-Not an Armstrong Number

Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those.

Matlab code

function armstrong_numberornot(x)

count=numberofdigits(x);%Function which already created

x_copy=x;

sum=0;

while x>0

    num=mod(x,10);

    sum=sum+num^count;

    x=floor(x/10);

   

end

if x_copy==sum

    fprintf('The given number is armstrong number');

else

    fprintf('The given number is not an armstrong Number');

end

end

 

Sample Input and Output
>> armstrong_numberornot(9474)
The given number is armstrong number>> armstrong_numberornot(378)
The given number is not an armstrong Number>> armstrong_numberornot(371)
The given number is armstrong number>> armstrong_numberornot(890)
The given number is not an armstrong Number>> armstrong_numberornot(0)
The given number is armstrong number>> armstrong_numberornot(1)
The given number is armstrong number>> armstrong_numberornot(2)
The given number is armstrong number>> armstrong_numberornot(11)
The given number is not an armstrong Number


Click here for Video Description



No comments

Popular Posts