NPTEL 2020: PROGRAMMING IN JAVA WEEK 1:Q4 ANSWER

Complete the code segment to check whether the number is an Armstrong number or not.

Armstrong Number:

A positive number is called an Armstrong number if it is equal to the sum of cubes of its digits for example 153 = 13+53+33, 370, 371, 407, etc.

int temp=n;
int c=0,t;
//Use while loop to check the number is Armstrong or not.
while(n>0)
{
t=n%10;
n=n/10;
c=c+(t*t*t);
}
if(temp==c)
result=1;
else
result=0;
//Evaluation code
System.out.println(result);

Leave a Reply

Your email address will not be published. Required fields are marked *