Check whether the number is armstrong or not | faceprep

Program to check whether the given number is Armstrong or not is discussed here. A number is an Armstrong number when the sum of nth power of each digit is equal to the number itself. Here n is the number of digits in the given number. For example,

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.

Algorithm

  1. Input the number.
  2. Initialize sum=0 and temp=number.
  3. Find the total number of digits in the number.
  4. Repeat until (temp != 0)
  5. remainder = temp % 10
  6. result = resut + pow(remainder,n)
  7. temp = temp/10
  8. if (result == number)
  9. Display “Armstrong”
  10. Else
  11. Display “Not Armstrong”

Program to check whether the number is Armstrong or not

// C program to check whether the given number is Armstrong or not

#include
#include

int main()
{
int number, temp, remainder, result = 0, n = 0 ;

printf(“Enter an integer: “);
scanf(“%d”, &number);

temp = number;

// Finding the number of digits

while (temp != 0)
{
temp /= 10;
++n;
}

temp = number;

// Checking if the number is armstrong

while (temp != 0)
{
remainder = temp%10;
result += pow(remainder, n);
temp /= 10;
}

if(result == number)
printf(“%d is an Armstrong number\n”, number);
else
printf(“%d is not an Armstrong number\n”, number);

return 0;
}

// C++ program to check whether the number is Armstrong or not

#include
#include using namespace std;

int main()
{
int number, temp, remainder, result = 0, n = 0 ;

cout << “Enter an integer : “;
cin >> number;

temp = number;

// Finding the number of digits

while (temp != 0)
{
temp /= 10;
++n;
}

temp = number;

// Checking if the number is armstrong

while (temp != 0)
{
remainder = temp%10;
result += pow(remainder, n);
temp /= 10;
}

if(result == number)
cout << number << ” is an Armstrong number\n”;
else
cout << number << ” is not an Armstrong number\n”;

return 0;
}

// Java program to check whether the number is Armstrong or not

import java.util.*;
public class sum_of_primes {

public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter a number: “);
int number = sc.nextInt();
int temp, remainder, result = 0, n = 0 ;
temp = number;

// Finding the number of digits

while (temp != 0)
{
temp /= 10;
++n;
}

temp = number;

// Checking if the number is armstrong

while (temp != 0)
{
remainder = temp%10;
result += Math.pow(remainder, n);
temp /= 10;
}

if(result == number)
System.out.print(number + ” is an Armstrong number\n”);
else
System.out.print(number + ” is not an Armstrong number\n”);

}

}

# Python program to check whether the number is Armstrong or not

import math
number = int(input(“Enter a number : “))
result = 0
n = 0
temp = number;
while (temp != 0):
temp =int(temp/10)
n = n + 1

// Checking if the number is armstrong

temp = number
while (temp != 0):
remainder = temp % 10
result = result + math.pow(remainder, n)
temp =int(temp/10)

if(result == number):
print(number,”is an Armstrong number”)
else:
print(number,”is not an Armstrong number”)

#include<stdio.h>
#include<math.h>
#include int main(int a, char*b[])
{
int n;
n= atoi(b[1]);
int sum=0;
int temp=n;
int cnt=0;
while(n!=0)
{
n=n/10;
cnt++;
}
n=temp;
while(n!=0)
{
int rem=n%10;
sum=sum+pow(rem,cnt);
n=n/10;
}
if(temp==sum)
{
printf(“yes”);
}
else
{
printf(“no”);
}
return 0;
}

Output:

armstrong or not

armstrong or not

Time complexity: O(n)