Check number is armstrong or not

Here given code implementation process.

//C Program
//Check number is armstrong or not
#include <stdio.h>

//Calculate power of given number
long pow_number(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 is_armstrong(long number)
{
  
  int size = 0;

  long temp = number , result = 0;
  //Get the length of digits
  while(temp!=0)
  {
    size++;

    temp/=10;
  }
  
  for (long i = number; i > 0 ; i/=10)
  {
    //Add power
    result += pow_number(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
  is_armstrong(153);

  is_armstrong(132);
  
  is_armstrong(1634);

  return 0;
}

Output

153 is armstrong number
132 is not armstrong number
1634 is armstrong number
/*
 C++ Program
 Check number is armstrong or not
*/
#include<iostream>

using namespace std;

class MyNumber {
	public:

		//Calculate power of given number
		long pow_number(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 is_armstrong(long number) {
		int size = 0;
		long temp = number, result = 0;
		//Get the length of digits
		while (temp != 0) {
			size++;
			temp /= 10;
		}
		for (long i = number; i > 0; i /= 10) {
			//Add power
			result += this->pow_number(i % 10, size);
		}
		if (result == number) {
			cout << number << " is armstrong number\n";
		} else {
			cout << number << " is not armstrong number \n";
		}
	}
};
int main() {
	MyNumber obj;
	//Test Case
	obj.is_armstrong(153);
	obj.is_armstrong(132);
	obj.is_armstrong(1634);
	return 0;
}

Output

153 is armstrong number
132 is not armstrong number
1634 is armstrong number
/*
  Java Program
  Check number is armstrong or not
*/
public class MyNumber {


  //Calculate power of given number
  public long pow_number(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 is_armstrong(long number)
  {
    
    int size = 0;

    long temp = number , result = 0;
    //Get the length of digits
    while(temp!=0)
    {
      size++;

      temp/=10;
    }
    
    for (long i = number; i > 0 ; i/=10)
    {
      //Add power
      result += pow_number(i%10,size);
    }
    if(result==number)
    {
      System.out.print(number+" is armstrong number\n");
    }
    else
    {
      System.out.print(number+" is not armstrong number \n");
    }
  }


  public static void main(String[] args) {

    MyNumber obj = new MyNumber();
    //Test Case
    obj.is_armstrong(153);

    obj.is_armstrong(132);
    
    obj.is_armstrong(1634);
  }
}

Output

153 is armstrong number
132 is not armstrong number
1634 is armstrong number
/*
  C# Program
  Check number is armstrong or not
*/
using System;
public class MyNumber {


	//Calculate power of given number
	public long pow_number(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 is_armstrong(long number) {

		int size = 0;

		long temp = number, result = 0;
		//Get the.Length of digits
		while (temp != 0) {
			size++;

			temp /= 10;
		}

		for (long i = number; i > 0; i /= 10) {
			//Add power
			result += pow_number(i % 10, size);
		}
		if (result == number) {
			Console.Write(number + " is armstrong number\n");
		} else {
			Console.Write(number + " is not armstrong number \n");
		}
	}


	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();
		//Test Case
		obj.is_armstrong(153);

		obj.is_armstrong(132);

		obj.is_armstrong(1634);
	}
}

Output

153 is armstrong number
132 is not armstrong number
1634 is armstrong number
# Python 3 Program
# Check number is armstrong or not
class MyNumber :
	# Calculate power of given number
	def pow_number(self, number, size) :
		if (number == 0) :
			return 0
		else :
			result = number
			i = 2
			while (i <= size) :
				result = result * number
				i += 1
			
			return result
		
	
	def is_armstrong(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.pow_number(i % 10, size)
			i = int(i / 10)
		
		if (result == number) :
			print(number ," is armstrong number")
		else :
			print(number ," is not armstrong number ")
		
	

def main() :
	obj = MyNumber()
	obj.is_armstrong(153)
	obj.is_armstrong(132)
	obj.is_armstrong(1634)


if __name__ == "__main__":
	main()

Output

153 is armstrong number
132 is not armstrong number
1634 is armstrong number
# Ruby Program 
# Check number is armstrong or not
class MyNumber 
	# Calculate power of given number
	def pow_number(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 is_armstrong(number) 
		size = 0
		temp = number
		result = 0
		# Get the length of digits
		while (temp != 0) 
			size += 1
			temp /= 10
		end
		i = number
		while (i > 0) 
			# Add power
			result += self.pow_number(i % 10, size)
			i /= 10
		end
		if (result == number) 
			print(number ," is armstrong number\n")
		else 
			print(number ," is not armstrong number \n")
		end
	end
end
def main() 
	obj = MyNumber.new()
	obj.is_armstrong(153)
	obj.is_armstrong(132)
	obj.is_armstrong(1634)
end
main()

Output

153 is armstrong number
132 is not armstrong number 
1634 is armstrong number
/*
 Scala Program
 Check number is armstrong or not
*/
class MyNumber {
	//Calculate power of given number
	def pow_number(number: Long, size: Int): Long = {
		if (number == 0) {
			return 0;
		} else {
			var result: Long = number;
			var i: Int = 2;
			while (i <= size) {
				result = result * number;
				i += 1;
			}
			return result;
		}
	}
	def is_armstrong(number: Long): Unit = {
		var size: Int = 0;
		var temp: Long = number;
		var result: Long = 0;
		//Get the length of digits
		while (temp != 0) {
			size += 1;
			temp = (temp / 10).toInt;
		}
		var i: Long = number;
		while (i > 0) {
			//Add power
			result += this.pow_number(i % 10, size);
			i = (i / 10).toInt;
		}
		if (result == number) {
			print(s"$number is armstrong number\n");
		} else {
			print(s"$number is not armstrong number \n");
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
        obj.is_armstrong(153);
        obj.is_armstrong(132);
        obj.is_armstrong(1634);
	}
}

Output

153 is armstrong number
132 is not armstrong number
1634 is armstrong number
/*
  Swift 4 Program
  Check number is armstrong or not
*/
class MyNumber {
	//Calculate power of given number
	func pow_number(_ 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 is_armstrong(_ 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 /= 10;
		}
		var i: Int = number;
		while (i > 0) {
			//Add power
			result += self.pow_number(i % 10, size);
			i /= 10;
		}
		if (result == number) {
			print(number ," is armstrong number");
		} else {
			print(number ," is not armstrong number ");
		}
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	obj.is_armstrong(153);
	obj.is_armstrong(132);
	obj.is_armstrong(1634);
}
main();

Output

153  is armstrong number
132  is not armstrong number
1634  is armstrong number
<?php
/*
  Php Program
  Check number is armstrong or not
*/
class MyNumber {
	//Calculate power of given number

	public 	function pow_number($number, $size) {
		if ($number == 0) {
			return 0;
		} else {
			$result = $number;
			for ($i = 2; $i <= $size; ++$i) {
				$result = $result *$number;
			}
			return $result;
		}
	}
	public 	function is_armstrong($number) {
		$size = 0;
		$temp = $number;
		$result = 0;
		//Get the length of digits
		while ($temp != 0) {
			$size++;
			$temp = intval($temp / 10);
		}
		for ($i = $number; $i > 0; $i = intval($i / 10)) {
			//Add power
			$result += $this->pow_number($i % 10, $size);
		}
		if ($result == $number) {
			echo($number ." is armstrong number\n");
		} else {
			echo($number ." is not armstrong number \n");
		}
	}
};

function main() {
	$obj = new MyNumber();
	//Test Case

	$obj->is_armstrong(153);
	$obj->is_armstrong(132);
	$obj->is_armstrong(1634);
}
main();

Output

153 is armstrong number
132 is not armstrong number
1634 is armstrong number
/*
 Node Js Program
 Check number is armstrong or not
*/
class MyNumber {
	//Calculate power of given number
	pow_number(number, size) {
		if (number == 0) {
			return 0;
		} else {
			var result = number;
			for (var i = 2; i <= size; ++i) {
				result = result *number;
			}
			return result;
		}
	}
	is_armstrong(number) {
		var size = 0;
		var temp = number;
		var result = 0;
		//Get the length of digits
		while (temp != 0) {
			size++;
			temp = parseInt(temp / 10);
		}
		for (var i = number; i > 0; i = parseInt(i / 10)) {
			//Add power
			result += this.pow_number(i % 10, size);
		}
		if (result == number) {
			process.stdout.write(number + " is armstrong number\n");
		} else {
			process.stdout.write(number + " is not armstrong number \n");
		}
	}
}

function main(args) {
	var obj = new MyNumber();
	//Test Case
	obj.is_armstrong(153);
	obj.is_armstrong(132);
	obj.is_armstrong(1634);
}
main();

Output

153 is armstrong number
132 is not armstrong number
1634 is armstrong number


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







© 2021, kalkicode.com, All rights reserved