Here, we have a basic program example to check if 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.
Code to check if number is Armstrong in C language
#include <stdio.h>
int main() {
int n, originalNum, r, res = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &n);
originalNum = n;
while (originalNum != 0) {
r = originalNum % 10;
res += r * r * r;
originalNum /= 10;
}
if (res == n)
printf("%d is an Armstrong number.", n);
else
printf("%d is not an Armstrong number.", n);
return 0;
}
Code to check if number is Armstrong in C++ language
#include <iostream>
using namespace std;
int main() {
int n, originalNum, r, res = 0;
cout << "Enter a three-digit integer: ";
cin >> n;
originalNum = n;
while (originalNum != 0) {
r = originalNum % 10;
res += r * r * r;
originalNum /= 10;
}
if (res == n)
cout << n << " is an Armstrong number.";
else
cout << n << " is not an Armstrong number.";
return 0;
}
Code to check if number is Armstrong in Python language
n = int(input("Enter a number: "))
sum = 0
temp = n
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if n == sum:
print(n,"is an Armstrong number")
else:
print(n,"is not an Armstrong number")
Code to check if number is Armstrong in Java language
import java.util.*;
public class armstrong {
public static void main(String[] args) {
int num, originalNumber, r, n = 0;
double res = 0.0;
System.out.println("Enter a positive integer: ");
Scanner s=new Scanner(System.in);
num = s.nextInt();
originalNumber = num;
while (originalNumber != 0)
{
r = originalNumber % 10;
res += Math.pow(r, 3);
originalNumber /= 10;
}
if(res == num)
System.out.println(num + " is an Armstrong number.");
else
System.out.println(num + " is not an Armstrong number.");
}
}