Armstrong Number2 months ago

The program must accept an integer N as the input. The program must print yes if the integer N is an Armstrong number. Else the program must print no as the output. An X digit integer is said to be an Armstrong number or Narcissistic number if the sum of each digit raised to the power X is equal to the same integer.

Boundary Condition(s):
1 <= N <= 10^8

Input Format:
The first line contains N.

Output Format:
The first line contains either yes or no.

Example Input/Output 1:
Input:
1634

Output:
yes

Explanation:
The number of digits is 4.
1^4 + 6^4 + 3^4 + 4^4
= 1 + 1296 + 81 + 256
= 1634

Example Input/Output 2:
Input:
250

Output:
no

Explanation:
The number of digits is 3.
2^3 + 5^3 + 0^3
= 8 + 125 + 0
= 133

The below video explains the logic and the implementation in C programming language to check if the given number is an Armstrong number or not.

#include <stdio.h>
#include <stdlib.h>

int power(int num, int pow)
{
int result = 1;
while(pow-->0)
{
result *= num;
}
return result;
}

int main()
{
int N, len;
scanf("%d%n",&N, &len);
int sum = 0, origN = N;
while(N != 0)
{
int unitDigit = N %10;
sum += power(unitDigit, len);
N /= 10;
}
printf(sum == origN ? "yes" : "no");
return 0;
}

#include <iostream>

using namespace std;

int power(int num, int pow)
{
int result = 1;
while(pow-- > 0)
{
result *= num;
}
return result;
}

int main()
{
int N;
cin >> N;
int len = to_string(N).length();
int sum = 0, origN = N;
while(N != 0)
{
int unitDig = N%10;
sum += power(unitDig, len);
N /= 10;
}
cout << (sum == origN ? "yes" : "no");
}

import java.util.Scanner;

public class Hello {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Integer N = sc.nextInt();
int sum = 0, origN = N;
int len = Integer.toString(N).length();
while (N != 0) {
int unitDig = N % 10;
sum += power(unitDig, len);
N /= 10;
}
System.out.print(sum == origN ? "yes" : "no");
}

private static int power(int num, int pow) {
int result = 1;
while (pow-- > 0) {
result *= num;
}
return result;
}
}

N = int(input())
length = len(str(N))
sumVal, origN = 0, N
while N > 0:
unitDig = N%10
sumVal += (unitDig ** length)
N //= 10
if sumVal == origN:
print('yes')
else:
print('no')