Armstrong Number

You are given an integer ‘NUM’ . Your task is to find out whether this number is an Armstrong number or not.

A k-digit number ‘NUM’ is an Armstrong number if and only if the k-th power of each digit sums to ‘NUM’.

Example
153 = 1^3 + 5^3 + 3^3.

Therefore 153 is an Armstrong number.
Input Format:
The first line contains an integer ‘T’, which denotes the number of test cases to be run. Then, the ‘T’ test cases follow.

The first line of each test case contains a single positive integer, ‘NUM’.
Output Format:
For each test case, print ‘YES’ if it is an Armstrong number otherwise print ‘NO’.

The output of each test case will be printed in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1 <= ‘T’ <= 100
1 <= ‘N’ <= 10^9

Time Limit: 1 sec
CodingNinjas
author
2y

class Solution {
static String armstrongNumber(int n){
int temp = n;
int arm = 0;
while(n!=0) {
int rem = n%10;
arm += Math.pow(rem,3);
n = n/10;
}
if(arm == temp) {
return "Yes";
}
else {
return "No";
}
}
}

CodingNinjas
author
2y
Maths
  • The first step will be to find the length of the number.
  • Initialize an integer k = 0, it will be used to find the length of the number.
  • Initialise an integer sum = 0.
  • Run a while loop till num is no...read more
Help your peers!
Add answer anonymously...
Optum Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter