THE GIVEN NUMBER IS ARMSTRONG OR NOT

THE GIVEN NUMBER IS ARMSTRONG OR NOT

INTRODUCTION

CHECK WHETHER THE GIVEN NUMBER IS ARMSTRONG OR NOT.

The Armstrong number of n digits is a number in which the sum of each digit of the number with power n is equal to the original number. The Armstrong number is also known as the Narcissist number. Let’s look at an example for a better understanding of the Armstrong number.

An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.

Example:
INPUT: 153
OUTPUT: ARMSTRONG NUMBER

Explanation

As the sum of each digit by the power of three is equal to the actual number, that is 153.

1^3+5^3+3^3 = 153

Some more examples of an Armstrong number are

0, 1, 153, 370, 371, 407, and 1634

Here is an example of a function that determines if a number is an Armstrong number

APPROACH1:

To check whether the given number is Armstrong or not, first, we will extract the count of the number of digits in the given number. Then we will extract each digit from the given number and the sum of each digit with the power of the number of digits.

If the sum is equal to the original number, then the number is considered an Armstrong number. Otherwise, it is not an Armstrong number.

See also  Lean 6 Sigma Green Belt professional Certification

ALGORITHM

Step 1: Start.
Step 2: Read an input number. Store it in two temporary variables, one if for counting the number of digits and another one is for storing the original number.
Step 3: Extract the last digit of the number by using the ‘%’ operator.
Step 4: Add to a temporary variable, let’s take it as sum, by using the formulae sum=sum+(digit^no.of digits)
Step 5: Check whether the original number and sum variable possess the same value or not by checking the temp variable and sum variable. If both are equal, then the number is an Armstrong number. Otherwise, it is not an Armstrong number.
Step 6: Stop.

C++ CODE:

#include <iostream>
#include<cmath>
using namespace std;

int main() {
    int n,digits=0,sum=0;
    cin>>n;
    int temp=n;
    int original=n;
    while(n>0){
      digits++;
      n=n/10;
    }
    while(temp>0){
      int rem=temp%10;
      sum+=pow(rem,digits);
      temp=temp/10;
    }
    if(sum == original) cout<<"ARMSTRONG NUMBER";
    else cout<<"NOT AN ARMSTRONG NUMBER";
}

JAVA CODE:

import java.util.*;
public class Main {
    public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
      int n = s.nextInt();
      int temp=n;
      int original=n;
      int sum=0,digits=0,rem=0;
      while(n>0){
      digits++;
      n=n/10;
    }
    while(temp>0){
      rem=temp%10;
      sum+=Math.pow(rem,digits);
      temp=temp/10;
    }
    if(sum == original) 
    System.out.println("ARMSTRONG NUMBER");
    else
    System.out.println("NOT AN ARMSTRONG NUMBER");
  }
}

PYTHON CODE:

n = int(input())
temp=n
original=n
digits=0
rem=0
sums=0
while n>0:
  digits+=1
  n=n//10
while temp>0:
  rem=temp%10
  sums=sums+pow(rem,digits)
  temp=temp//10
if(sums == original):
  print("ARMSTRONG NUMBER")
else:
  print("NOT AN ARMSTRONG NUMBER")
Input 1: 123
Output 1: NOT AN ARMSTRONG NUMBER.

Input 2: 1634
Output 2: ARMSTRONG NUMBER.

The time complexity of the code is O(N), where n is the number of digits since we need to traverse every digit and add digits raised to power no. of digits to sum.

See also  10 amazing project ideas for Java Programming for beginners

The space complexity of the code is O(1).

Another way using python

def is_armstrong(num):
    num_str = str(num)
    num_len = len(num_str)
    return num == sum(int(digit) ** num_len for digit in num_str)

An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.

Here is an example of a function that determines if a number is an Armstrong number in Python:

def is_armstrong(num):
    num_str = str(num)
    num_len = len(num_str)
    return num == sum(int(digit) ** num_len for digit in num_str)


In this example, the function first converts the number to a string and stores the length of the string in a variable. Then it uses a list comprehension to calculate the sum of each digit raised to the power of the length of the number. Finally, it compares this value to the original number and returns True if they are equal and False otherwise.

CONCLUSION

That’s it from this tutorial. Hope you guys found It interesting. We have solved the problem to check whether the given number is an Armstrong number or not in different programming languages by using different numbers. Happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock