Narcissistic( Armstrong) Number using MATLAB


Given a number x, determine whether the given number is Narcissistic Number (Armstrong number) or not.

 A positive integer of n digits is called an Armstrong number of order n (order is number of digits) if.
abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + ....
Input : 153
Output : Yes
153 is an Armstrong number.
1*1*1 + 5*5*5 + 3*3*3 = 153

Input : 120
Output : No
120 is not a Armstrong number.
1*1*1 + 2*2*2 + 0*0*0 = 9

Input : 1253
Output : No
1253 is not a Armstrong Number
1*1*1*1 + 2*2*2*2 + 5*5*5*5 + 3*3*3*3 = 723

Input : 1634
Output : Yes
1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4 = 1634

If you want to know more about Narcissistic number , check the link given below:



Code:

clc;

x=input('Enter the number:');
c=0;
m=x;
while(m>0)
    b=rem(m,10);
    c=c+1;
    m=(m-b)/10;
end
rs=0;
m=x;
while(m>0)
    b=rem(m,10);
    rs=rs+(b^c);
    m=(m-b)/10;
end
if(rs==x)
    disp('Yes');
else
    disp('No');
end

Output:



Explanation:



Function Implementation:
[Simply return 1 (true) if a supplied number is narcissistic or 0 (false) if not.]


function a=ece(x);
c=0;
m=x;
while(m>0)
    b=rem(m,10);
    c=c+1;
    m=(m-b)/10;
end
rs=0;
m=x;
while(m>0)
    b=rem(m,10);
    rs=rs+(b^c);
    m=(m-b)/10;
end
if(rs==x)
    a=1;
else
    a=0;
end
end

Output:




Reference Video(How to extract digits from a number in MATLAB):



No comments