Program to identify if the number is Armstrong number or not

Program to identify if the number is Armstrong number or not

10 May 2023

10 May 2023

Write a program to identify if the number is Armstrong number or not



Description about Program to identify if the number is Armstrong number or not

Get an input number from user and check whether the given number is an Armstrong number or not.

E.g.  Let the number be 1634,

 Here 1^4 + 6^4 + 3 ^4 + 4^4 = 1634

Therefore, this is an Armstrong number

Input

153

Output

Armstrong number

Input

121

Output

Not an Armstrong number

C Program to identify if the number is Armstrong number or not

#include <stdio.h>

#include<math.h>

int main()

{

    int num, copy, rem, result = 0, n = 0 ; 

    printf("Enter a number: ");

    scanf("%d", &num);

    copy = num;

    while (num != 0)

    {

        num /= 10;

        n++;

    }

    num = copy;    

    while (num != 0)    

    {

        rem = num%10;

        result += pow(rem, n);

        num /= 10;    

    }

    if(result == copy)

        printf("Armstrong number");    

    else

        printf("Not an Armstrong number");

return 0;

}

C++ Program to identify if the number is Armstrong number or not

#include <iostream>

#include<math.h>

using namespace std;

int main()

{

    int num, copy, rem, result = 0, n = 0 ; 

    cout<<"Enter a number: ";

    cin>>num;

    copy = num;

    while (num != 0)

    {

        num /= 10;

        n++;

    }

    num = copy;    

    while (num != 0)    

    {

        rem = num%10;

        result += pow(rem, n);

        num /= 10;    

    }

    if(result == copy)

        cout<<"Armstrong number";    

    else

        cout<<"Not an Armstrong number";

return 0;

}

Java Program to identify if the number is Armstrong number or not

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number : ");  

        int num = sc.nextInt(); 

        int copy = num;

        int result=0;

        int n=0;

        int rem;

    while (num != 0)

    {

        num /= 10;

        n++;

    }

    num = copy;    

    while (num != 0)    

    {

        rem = num%10;

        int mul=1;

     for(int i=1;i<=n;i++) {

      mul=mul*rem; 

    } 

    result=result+mul;

        num /= 10;    

    }

    if(result==copy)

    System.out.println("Armstrong Number");

    else

    System.out.println("Not Armstrong number");

}

}

Python Program to identify if the number is Armstrong number or not

import math

num=int(input("Enter a number: "))

value = [int(d) for d in str(num)]

result = 0

for i in range(0, len(value)):

    result = result + math.pow(value[i], len(value))

if result==num:

    print("Armstrong number")

else:

    print("Not an armstrong number")



Related Articles

Ask Us Anything !