Skip to main content

Check number is armstrong or not

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)
		{
			// Add power
			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)
	{
		Numbers task = new Numbers();
		// Test Case
		task.isArmstrong(153);
		task.isArmstrong(132);
		task.isArmstrong(1634);
	}
}
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)
        {
            // Add power
            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){
    Numbers task;
    // Test Case
    task.isArmstrong(153);
    task.isArmstrong(132);
    task.isArmstrong(1634);
    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)
    {
        // Add power
        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 {
        // Add power
        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)
        {
            // Add power
            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)
    {
        var task = new Numbers();
        // Test Case
        task.isArmstrong(153);
        task.isArmstrong(132);
        task.isArmstrong(1634);
    }
}
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)
        {
            // Add power
            $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)
    {
        $task = new Numbers();
        // Test Case
        $task->isArmstrong(153);
        $task->isArmstrong(132);
        $task->isArmstrong(1634);
    }
}
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))
        {
            // Add power
            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
var task = new Numbers();
// Test Case
task.isArmstrong(153);
task.isArmstrong(132);
task.isArmstrong(1634);
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))
        {
            // Add power
            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()
    {
        var task = new Numbers();
        // Test Case
        task.isArmstrong(153);
        task.isArmstrong(132);
        task.isArmstrong(1634);
    }
}
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) :
            # Add power
            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__":
    task = Numbers()
    # Test Case
    task.isArmstrong(153)
    task.isArmstrong(132)
    task.isArmstrong(1634)
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)
            # Add power
            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
task = Numbers.new()
# Test Case
task.isArmstrong(153)
task.isArmstrong(132)
task.isArmstrong(1634)
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)
        {
            // Add power
            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=
    {
        var task = new Numbers()
        // Test Case
        task.isArmstrong(153)
        task.isArmstrong(132)
        task.isArmstrong(1634)
    }
}
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)
        {
            // Add power
            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");
        }
    }
}

let task: Numbers = Numbers();
// Test Case
task.isArmstrong(153);
task.isArmstrong(132);
task.isArmstrong(1634);
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)
		{
			// Add power
			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
{
	val task: Numbers = Numbers();
	// Test Case
	task.isArmstrong(153);
	task.isArmstrong(132);
	task.isArmstrong(1634);
}
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.

New Comment