Program To Check Armstrong Number

Description

A number is an Armstrong Number if it is equal to the sum of
its own digits raised to the power of the number of digits.
Ex: 1,2,3,4,5,6,7,8,9,153,370,371,407,1634,8208,9474

C/C++

/* C Program to check Armstrong Number*/
//Save it as ArmstrongNumber.c

#include<stdio.h>
#include<math.h>

int main(){

    int i, num, count=0, remainder, temp;
    double sum=0.0;

    printf("Enter a number : ");
    scanf("%d",&num);

    //Storing number in a temporary variable
    temp = num; 

    //Finding number of digits in the given number
    while(num>0){
        remainder = num%10;
        count++;
        num/=10;
    }

    //Restoring the number
    num = temp;

    //Calculating the power of individual digit
    while(num>0){
        remainder = num%10;
        sum += pow(remainder,count);
        num/=10;
    }

    //Restoring the number
    num = temp;

    /*Checking the total sum power of individual digit
     is equal to number or not*/
    if((int)sum == num){
        printf("%d is Armstrong number",num);
    }else{
        printf("%d is not Armstrong number",num);
    }

    return 0;
}
Input:
Enter a number : 1634

Output:
1634 is Armstrong number

Java

/* Java Program to check Armstrong Number*/
//Save it as ArmstrongNumber.java

import java.io.*;
import java.util.Scanner;

public class ArmstrongNumber {

    public static void main(String[] args) {

            Scanner scanner = new Scanner(System.in);
        
            int i, num, count=0, remainder, temp;
        double sum=0.0;

        System.out.println("Enter a number : ");
        num = scanner.nextInt();

        //Storing number in a temporary variable
        temp = num; 

        //Finding number of digits in the given number
        while(num > 0){
            remainder = num%10;
            count++;
            num/=10;
        }

        //Restoring the number
        num = temp;

        //Calculating the power of individual digit
        while(num > 0){
            remainder = num%10;
            sum += Math.pow(remainder,count);
            num/=10;
        }

        //Restoring the number
        num = temp;

        /*Checking the total sum power of individual digit
         is equal to number or not*/
        if((int)sum == num){
            System.out.println(num+" is Armstrong number");
        }else{
        	System.out.println(num+" is not Armstrong number");
        }
    }
}
Input:
Enter a number : 
1634

Output:
1634 is Armstrong number

Related Programs

1) Program to Check Whether a Number is Prime or Not
2) Program to Check Whether a Character is a Vowel or Not
3) Program to Check Whether a Number is Even or Odd
4) Check whether a given number is a perfect number or not
5) Program to Check Leap Year
6) Program to calculate Volume and Surface Area of Sphere
7) Program to add two complex numbers
8) Program to find HCF using Recursion
9) Program to calculate square root of a number without using standard library function sqrt()
10) Program to find HCF(Highest Common Factor)/GCD(Greatest Common Divisor) and LCM(Least Common Multiple)
Share Me

Leave a Reply