Posted on by Kalkicode
Code Number

# Check number is armstrong or not

An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153. Another example is 371, which is also an Armstrong number, because 3^3 + 7^3 + 1^3 = 371. Armstrong numbers are named after Michael F. Armstrong, who first introduced them in 1969.

## Armstrong number program in java

``````/*
Java program for
Check number is armstrong or not
*/
public class Numbers
{
// Calculate power of given number
public long power(long number, int size)
{
if (number == 0)
{
return 0;
}
else
{
long result = number;
for (int i = 2; i <= size; ++i)
{
result = result * number;
}
return result;
}
}
public void isArmstrong(long number)
{
int size = 0;
long temp = number, result = 0;
// Get the length of digits
while (temp != 0)
{
size++;
temp = temp /10;
}
for (long i = number; i > 0; i /= 10)
{
result += power(i % 10, size);
}
if (result == number)
{
System.out.println(number + " is armstrong number");
}
else
{
System.out.println(number + " is not armstrong number");
}
}
public static void main(String[] args)
{
// Test Case
}
}``````
Output
``````153 is armstrong number
132 is not armstrong number
1634 is armstrong number``````

## Armstrong number program in c++

``````// Include header file
#include <iostream>

//  Stdc++11 program for
//  Check number is armstrong or not
class Numbers
{
// Calculate power of given number
public:
long power(long number, int size)
{
if (number == 0)
{
return 0;
}
else
{
long result = number;
for (int i = 2; i <= size; ++i)
{
result = result * number;
}
return result;
}
}
void isArmstrong(long number)
{
int size = 0;
long temp = number;
long result = 0;
// Get the length of digits
while (temp != 0)
{
size++;
temp = temp / 10;
}
for (long i = number; i > 0; i /= 10)
{
result += power(i % 10, size);
}
if (result == number)
{
std::cout << number
<<" is armstrong number"
<< std::endl;
}
else
{
std::cout << number
<< " is not armstrong number"
<< std::endl;
}
}
};
int main(int argc, char **argv){
// Test Case
return 0;
};``````
Output
``````153 is armstrong number
132 is not armstrong number
1634 is armstrong number``````

## Armstrong number program in c

``````// Include header file
#include <stdio.h>
//  C program for
//  Check number is armstrong or not

// Calculate power of given number
long power (long number, int size)
{
if (number == 0)
{
return 0;
}
else
{
long result = number;
for (int i = 2; i <= size; i++)
{
result = result * number;
}
return result;
}
}
void  isArmstrong (long number)
{
int size = 0;
long temp = number;
long result = 0;
// Get the length of digits
while(temp != 0)
{
size++;
temp = temp / 10;
}
for (long i = number; i > 0; i /= 10)
{
result += power(i % 10, size);
}
if (result == number)
{
printf("%ld is armstrong number\n",number);
}
else
{
printf("%ld is not armstrong number\n",number);
}
}

int main()
{
// Test Case
isArmstrong(153);
isArmstrong(132);
isArmstrong(1634);
return 0;
}``````
Output
``````153 is armstrong number
132 is not armstrong number
1634 is armstrong number``````

## Armstrong number program in golang

``````package main
import "fmt"

//  Golang program for
//  Check number is armstrong or not

// Calculate power of given number
func power( number  int64,  size  int)int64 {
if (number == 0) {
return 0;
} else {
var  result  int64 = number;
for  i := 2; i <= size; i++ {
result = result * number;
}
return result;
}
}
func isArmstrong( number  int64) {
var  size  int = 0;
var  temp  int64 = number;
var  result  int64 = 0;
// Get the length of digits
for(temp != 0) {
size++;
temp = temp / 10;
}
for  i  := number; i > 0; i /= 10 {
result += power(i % 10, size);
}
if (result == number) {
fmt.Printf("%d is armstrong number\n",number);
} else {
fmt.Printf("%d  is not armstrong number\n",number);
}
}
func main() {

// Test Case
isArmstrong(153);
isArmstrong(132);
isArmstrong(1634);

}``````
Output
``````153 is armstrong number
132 is not armstrong number
1634 is armstrong number``````

## Armstrong number program in c#

``````// Include namespace system
using System;
//  C# program for
//  Check number is armstrong or not
public class Numbers
{
// Calculate power of given number
public long power(long number, int size)
{
if (number == 0)
{
return 0;
}
else {
var result = number;
for (int i = 2; i <= size; ++i)
{
result = result * number;
}
return result;
}
}
public void isArmstrong(long number)
{
var size = 0;
var temp = number;
long result = 0;
// Get the length of digits
while (temp != 0)
{
size++;
temp = temp / 10;
}
for (long i = number; i > 0; i /= 10)
{
result += this.power(i % 10, size);
}
if (result == number)
{
Console.WriteLine(number + " is armstrong number");
}
else {
Console.WriteLine(number + " is not armstrong number");
}
}
public static void Main(String[] args)
{
// Test Case
}
}``````
Output
``````153 is armstrong number
132 is not armstrong number
1634 is armstrong number``````

## Armstrong number program in php

``````<?php
//  Php program for
//  Check number is armstrong or not
class Numbers
{
// Calculate power of given number
function power(\$number, \$size)
{
if (\$number == 0)
{
return 0;
}
else
{
\$result = \$number;
for (\$i = 2; \$i <= \$size; ++\$i)
{
\$result = \$result * \$number;
}
return \$result;
}
}
function isArmstrong(\$number)
{
\$size = 0;
\$temp = \$number;
\$result = 0;
// Get the length of digits
while (\$temp != 0)
{
\$size += 1;
\$temp = (int)(\$temp / 10);
}
for (\$i = \$number; \$i > 0;
\$i = \$i / 10)
{
\$result += \$this->power(\$i % 10, \$size);
}
if (\$result == \$number)
{
echo \$number," is armstrong number\n";
}
else
{
echo \$number," is not armstrong number\n";
}
}
function main(&\$args)
{
// Test Case
}
}
Numbers::main(\$argv);``````
Output
``````153 is armstrong number
132 is not armstrong number
1634 is armstrong number
``````

## Armstrong number program in node js

``````//  Node Js program for
//  Check number is armstrong or not
class Numbers
{
// Calculate power of given number
power(number, size)
{
if (number == 0)
{
return 0;
}
else
{
var result = number;
for (var i = 2; i <= size; ++i)
{
result = result * number;
}
return result;
}
}
isArmstrong(number)
{
var size = 0;
var temp = number;
var result = 0;
// Get the length of digits
while (temp != 0)
{
size += 1;
temp = parseInt(temp / 10);
}
for (var i = number; i > 0; i = parseInt(i / 10))
{
result += this.power(i % 10, size);
}
if (result == number)
{
console.log(number + " is armstrong number");
}
else
{
console.log(number + " is not armstrong number");
}
}
}
// Start program execution
// Test Case
Output
``````153 is armstrong number
132 is not armstrong number
1634 is armstrong number``````

## Armstrong number program in typescript

``````//  Typescript program for
//  Check number is armstrong or not
class Numbers
{
// Calculate power of given number
public number power(number:number, size:number)
{
if (number == 0)
{
return 0;
}
else
{
var result = number;
for (var i = 2; i <= size; ++i)
{
result = result * number;
}
return result;
}
}
public  isArmstrong(num:number)
{
var size = 0;
var temp = num;
var result = 0;
// Get the length of digits
while (temp != 0)
{
size += 1;
temp = parseInt(temp / 10);
}
for (var i = num; i > 0; i = parseInt(i / 10))
{
result += this.power(i % 10, size);
}
if (result == num)
{
console.log(num + " is armstrong number");
}
else
{
console.log(num + " is not armstrong number");
}
}
public static  main()
{
// Test Case
}
}
Numbers.main([]);
/*
file : code.ts
tsc --target es6 code.ts
node code.js
*/``````
Output
``````153 is armstrong number
132 is not armstrong number
1634 is armstrong number``````

## Armstrong number program in python

``````#  Python 3 program for
#  Check number is armstrong or not
class Numbers :
# Calculate power of given number
def  power(self, number,  size) :
if (number == 0) :
return 0
else :
result = number
i = 2
while (i <= size) :
result = result * number
i += 1
return result
def isArmstrong(self, number) :
size = 0
temp = number
result = 0
# Get the length of digits
while (temp != 0) :
size += 1
temp = int(temp / 10)
i = number
while (i > 0) :
result += self.power(i % 10, size)
i = int(i / 10)
if (result == number) :
print(number, " is armstrong number")
else :
print(number, " is not armstrong number")

if __name__=="__main__":
# Test Case
Output
``````153  is armstrong number
132  is not armstrong number
1634  is armstrong number``````

## Armstrong number program in ruby

``````#  Ruby program for
#  Check number is armstrong or not
class Numbers
# Calculate power of given number
def power( number,  size)
if (number == 0)
return 0
else
result = number
i = 2
while (i <= size)
result = result * number
i += 1
end
return result
end
end
def isArmstrong( number)
size = 0
temp = number
result = 0
# Get the length of digits
while (temp != 0)
size += 1
temp = temp / 10
end
i = number
while (i > 0)
result += self.power(i % 10, size)
i = i / 10
end
if (result == number)
print(number," is armstrong number\n")
else
print(number," is not armstrong number\n")
end
end
end
# Test Case
Output
``````153 is armstrong number
132 is not armstrong number
1634 is armstrong number
``````

## Armstrong number program in scala

``````//  Scala program for
//  Check number is armstrong or not
class Numbers ()
{
// Calculate power of given number
def power(number : Long, size : Int) : Long=
{
if (number == 0)
{
return 0
}
else
{
var result = number
var i = 2
while (i <= size)
{
result = result * number
i += 1
}
return result
}
}
def isArmstrong(number : Long) : Unit=
{
var size = 0
var temp = number
var result : Long = 0
// Get the length of digits
while (temp != 0)
{
size += 1
temp = temp / 10
}
var i = number
while (i > 0)
{
result += (power(i % 10, size))
i = i / 10
}
if (result == number)
{
println(""+ number + " is armstrong number")
}
else
{
println(""+ number + " is not armstrong number")
}
}
}

object Main
{
def main(args : Array[String]) : Unit=
{
// Test Case
}
}``````
Output
``````153 is armstrong number
132 is not armstrong number
1634 is armstrong number``````

## Armstrong number program in swift

``````import Foundation
//  Swift program for
//  Check number is armstrong or not
class Numbers
{
// Calculate power of given number
func power(_ number: Int, _ size: Int) -> Int
{
if (number == 0)
{
return 0;
}
else
{
var result: Int = number;
var i: Int = 2;
while (i <= size)
{
result = result * number;
i += 1;
}
return result;
}
}
func isArmstrong(_ number: Int)
{
var size: Int = 0;
var temp: Int = number;
var result: Int = 0;
// Get the length of digits
while (temp != 0)
{
size += 1;
temp = temp / 10;
}
var i: Int = number;
while (i > 0)
{
result += self.power(i % 10, size);
i = i / 10;
}
if (result == number)
{
print(String(number) +
" is armstrong number");
}
else
{
print(String(number) +
" is not armstrong number");
}
}
}

// Test Case
Output
``````153 is armstrong number
132 is not armstrong number
1634 is armstrong number``````

## Armstrong number program in kotlin

``````//  Kotlin program for
//  Check number is armstrong or not
class Numbers
{
// Calculate power of given number
fun power(number: Long, size: Int): Long
{
if (number == 0.toLong())
{
return 0;
}
else
{
var result: Long = number;
var i: Int = 2;
while (i <= size)
{
result = result * number;
i += 1;
}
return result;
}
}
fun isArmstrong(number: Long): Unit
{
var size: Int = 0;
var temp: Long = number;
var result: Long = 0;
// Get the length of digits
while (temp != 0.toLong())
{
size += 1;
temp = temp / 10;
}
var i: Long = number;
while (i > 0)
{
result += this.power(i % 10, size);
i = i / 10;
}
if (result == number)
{
println(number.toString() + " is armstrong number");
}
else
{
println(number.toString() + " is not armstrong number");
}
}
}
fun main(args: Array < String > ): Unit
{
// Test Case
}``````
Output
``````153 is armstrong number
132 is not armstrong number
1634 is armstrong number``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post