Program to Check Armstrong Number [C, C++, Python & Java]

Posted in

Program to Check Armstrong Number [C, C++, Python & Java]
vinaykhatri

Vinay Khatri
Last updated on April 26, 2024

    This tutorial helps you know how to write a program to check whether a number is Armstrong or not. We will implement this program in C, C++, Python, and Java.

    To write the program for the Armstrong number, you need to know the syntax of a programming language, while loop, if...else statement, and logic for an Armstrong number.

    What is an Armstrong Number?

    An Armstrong number is a number with n digits whose sum of digits raised to the power n equals itself.

    For example, 153 is an Armstrong number because it is a 3-digit number, and the sum of each digit raised to the power 3 equals 153.

    Here is an illustration:

    (1) 3 +(5) 3 +(3) 3 = 1+125+27 = 153

    C Program to Check Armstrong Number

    #include<stdio.h>
    #include<math.h>
    int main()
    {
       int num, m=0, power=0, x,temp;
       printf("Enter a Number: ");
       scanf("%d", &num);
       temp = num;
       while(temp!=0)
       {
          temp = temp/10;
          power++;
       }
       temp = num;
       while(num!=0)
       {
          x =num%10;
          m+= pow(x,power);
          num= num/10;
       }
       if(temp == m)
       printf("%d is an Armstrong number", temp);
       else
       printf("%d is not an Armstrong number", temp);
       return 0;
    }

    Output

    Enter a Number: 153
    153 is an Armstrong number

    C++ Program to Check Armstrong Number

    #include<iostream>
    #include<cmath>
    using namespace std;
    int main()
    {
       int num, m=0, power=0, x,temp;
       cout<<"Enter a Number: ";
       cin>>num;
       temp = num;
       while(temp!=0)
       {
          temp = temp/10;
          power++;
       }
       temp = num;
       while(num!=0)
       {
          x =num%10;
          m+= pow(x,power);
          num= num/10;
       }
       if(temp == m)
       cout<<temp <<" is an Armstrong number";
       else
       cout<<temp <<" is not an Armstrong number";
       return 0;
    }

    Output

    Enter a number: 54748
    54748 is an Armstrong number

    Python Program to Check Armstrong Number

    num = int(input("Enter a number: "))
    temp = num
    power = 0
    m = 0
    while(temp!=0):
        temp = temp//10
        power += 1
    
    temp = num
    while(num!=0):
        x = num%10
        m += x**power
        num = num//10
    
    if temp==m:
        print(temp,"is an Armstrong number")
    
    else:
        print(temp,"is not an Armstrong number")

    Output

    Enter a number: 153
    153 is an Armstrong number

    Java Program to Check Armstrong Number

    import java.util.*;
    import java.lang.*;
    public class Main
    {
       public static void main(String[] args)
       {
       int num, m=0, power=0, x,temp;
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter a Number: ");
       num = sc.nextInt();
       temp = num;
       while(temp!=0)
       {
          temp = temp/10;
          power++;
       }
       temp = num;
       while(num!=0)
       {
          x =num%10;
          m+= Math.pow(x,power);
          num= num/10;
       }
       if(temp == m)
       System.out.println(+temp+ " is an Armstrong number");
       else
       System.out.println(+temp+ " is not an Armstrong number");
       }
    }

    Output

    Enter a Number: 
    345
    345 is not an Armstrong number

    Conclusion

    This was all about writing a program for Armstrong number in C, C++, Python, and Java. Once you know the logic of how to determine an Armstrong number, you can easily write and run the program. In addition, you must take care of the while loop to separate the digits of a number.

    Refer to the above programs, and implement them in your system. If you face any difficulties, let us know via comments.

    People are also reading:

    Leave a Comment on this Post

    0 Comments