Program to check whether the number is Armstrong or not (for n numbers).

Here, we have a basic program example to check if the number is Armstrong or not using different languages. Armstrong number is the number in any given number base, which forms the total of the same number, when each of its digits is raised to the power of the number of digits in the number. This program is created in c language, c++, Java, and Python.

Program to check for Armstrong number in C language

#include <stdio.h>
int main() {
   int num, originalNum, r, n = 0;
   float res = 0.0;
   printf("Enter an integer: ");
   scanf("%d", &num);
   originalNum = num;
   for (originalNum = num; originalNum != 0; ++n) {
       originalNum /= 10;
   }
   for (originalNum = num; originalNum != 0; originalNum /= 10) {
       r = originalNum % 10;
      res += pow(r, n);
   }
   if ((int)res == num)
    printf("%d is an Armstrong number.", num);
   else
    printf("%d is not an Armstrong number.", num);
   return 0;
}

Program to check for Armstrong number in C++ language

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
   int num, originalNum, r, n = 0;
   float res = 0.0;
   cout<<"Enter an integer: ";
   cin>>num;
   originalNum = num;
   for (originalNum = num; originalNum != 0; ++n) {
       originalNum /= 10;
   }
   for (originalNum = num; originalNum != 0; originalNum /= 10) {
       r = originalNum % 10;
      res += pow(r, n);
   }
   if ((int)res == num)
    cout<<num<<" is an Armstrong number.";
   else
    cout<<num<<" is not an Armstrong number.";
   return 0;
}

Program to check for Armstrong number in Python language

num = int(input("Enter a number: "))
order = len(str(num))
sum = 0
temp = num
while temp > 0:
   digit = temp % 10
   sum += digit ** order
   temp //= 10

if num == sum:
   print(num,"is an Armstrong number")
else:
   print(num,"is not an Armstrong number")

Program to check for Armstrong number in Java language

import java.util.*;
public class armstrong {
    public static void main(String[] args) {     
       int num, originalNum, r, n = 0;
       double res = 0.0;
	System.out.println("Enter a positive integer:  ");
        Scanner s=new Scanner(System.in);
        num = s.nextInt();
	 originalNum = num;
	   for (originalNum = num; originalNum != 0; ++n) {
	       originalNum /= 10;
	   }
	   for (originalNum = num; originalNum != 0; originalNum /= 10) {
	       r = originalNum % 10;
	      res +=Math.pow(r, n);
	   }
	   if ((int)res == num)
	    System.out.println(num + " is an Armstrong number.");
	   else
	    System.out.println(num + " is not an Armstrong number.");       
    }
}