Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Check Armstrong

Moderate
0/80
271 upvotes

Problem statement

You are given an integer 'n'. Return 'true' if 'n' is an Armstrong number, and 'false' otherwise.


An Armstrong number is a number (with 'k' digits) such that the sum of its digits raised to 'kth' power is equal to the number itself. For example, 371 is an Armstrong number because 3^3 + 7^3 + 1^3 = 371.

Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line of the input contains an integer 'n'.


Output Format :
Return 'true' or 'false' as mentioned in the problem statement.


Note
You don't need to print anything, just implement the given function. "true" will be printed if the returned value is a boolean 'true' and "false" otherwise.
Sample Input 1 :
1


Sample Output 1 :
true


Explanation of Sample Input 1 :
1 is an Armstrong number as, 1^1 = 1.


Sample Input 2 :
103


Sample Output 2 :
false


Sample Input 3 :
1634


Sample Output 3 :
true


Explanation of Sample Input 3 :
1634 is an Armstrong number, as 1^4 + 6^4 + 3^4 + 4^4 = 1634


Check Armstrong
All tags
Sort by
Search icon

Interview problems

Easy c++ Solution with explanation.

•Better than 100%
•Runtime is 6ms
•Time Complexity is O(log n)



bool checkArmstrong(int n) {
// Store the original value of n in p for later comparison
int p = n;

// Initialize sum to 0, which will hold the sum of digits raised to the power of the number of digits
int sum = 0;

 /*Calculate the number of digits in the original number p. log10(p) gives the logarithm base 10 of p, adding 1 gives the total number of digits.*/
int count = int(log10(p) + 1); 
    
// Loop while n is greater than 0 (i.e., while there are still digits to process)
while (n > 0) {

// Get the last digit of n using the modulus operator
int ld = n % 10; 
        
// Raise the last digit (ld) to the power of the number of digits (count)
// and add this value to the sum.
sum = sum + pow(ld, count); 
        
// Remove the last digit from n by performing integer division by 10.
 n = n / 10; 
    }
    
// After processing all digits, check if the calculated sum equals the original number
    if (sum == p) {
    
 // If the sum equals the original number, it is an Armstrong number
        return true;
         
    } else {
    // Otherwise, it is not an Armstrong number
        return false; 
    }
}

programming

datascience

100daysofcode

37 views
0 replies
1 upvote

Interview problems

solution

bool checkArmstrong(int n){

    //Write your code here

    int original=n;

    int last=0;

    int sum=0;

    int count=0;

    

    while(n>0){

        last=n%10;

        count=count+1;

        n=n/10;

    }

    n=original;

    while(n>0){

        last=n%10;

        sum=sum+pow(last,count);

        n=n/10;

        

    }

    if(sum==original){

        return true;

    }

    else{

        return false;

    }

        

}

82 views
0 replies
1 upvote

Interview problems

c++ code

bool checkArmstrong(int n){
	int arm = 0;
	int org = n;

	int temp = n;
	int digits = 0;
	while(temp!=0){
		temp/=10;
		digits++;
	}

	while (n!=0){
		int rem = n%10;
		int power=1;
		for(int i=0;i<digits;i++){
			power *= rem;
		}
		arm += power;
		n/=10;
	}
	return org == arm;
}
81 views
0 replies
0 upvotes

Interview problems

Simple java code

import java.util.*;
public class Main {
	
	public static void main(String[] args) {
		// Write your code here
		Scanner sc = new Scanner(System.in);
		int n= sc.nextInt();

		int num = n;

		String strNum = String.valueOf(num);
		int len = strNum.length();

		double sum=0;
		while(n>0){
			int rem = n%10;
			sum=sum+Math.pow(rem, len);
			n=n/10;
		}
		if(sum==num){
			System.out.println("true");
		}else{
			System.out.println("false");
		}

	}
}

datastructures

Armstrong Number

Check Armstrong

131 views
0 replies
0 upvotes

Interview problems

c++ Armstrong soln

bool checkArmstrong(int n){

//Write your code here

int dgt=(int)(log10(n)+1);

long arm_sum=0;

int x=n;

for(int i=0;i<dgt;i++){

arm_sum+=pow(x%10,dgt);

x=x/10;

}

if(arm_sum==n) return true;

else return false;

}

79 views
0 replies
1 upvote

Interview problems

C++ solution

bool checkArmstrong(int n){

    //Write your code here

    int t,d,x,s=0;

    t=n; 

    x=int(log10(n)+1);

        while(n>0)

        {

            d=n%10;

            n=n/10;

            s=s+pow(d,x);

        }

        if(s==t)

        {

        return true;

        }

        return false;

}

 

44 views
0 replies
0 upvotes

Interview problems

Solution (very easy understanding):

bool checkArmstrong(int n){ 

    int dup = n;

    int dup1 = n;

    int sum = 0;

    int digits = 0;

 

    while(dup1>0){

        int ld = n % 10;

        dup1 = dup1/10;

        digits++;

    }

 

    while(n>0){

        int last_digit = n % 10;

        int s_pow = pow(last_digit,digits);

        n = n/10;

        sum = sum + s_pow;

    }    

    if(sum == dup) return true;

    else return false;

}

 

58 views
0 replies
1 upvote

Interview problems

Armstrong Cpp east

bool checkArmstrong(int n){

//Write your code here

int ori = n; // to keep up the original value for compare

int sum = 0;

 

int k = 0; // for the power number

while (n>0){

n /= 10;

k++;

}

 

n = ori; //change to ori

 

while(n>0){

int dig = n%10; //to extract the numbers

sum += pow(dig, k); //sum of the number raised to power

n /=10; // remove the number

}

 

if(sum == ori){

return true;

}

return false;

 

}

 

31 views
0 replies
0 upvotes

Interview problems

Shortest Code Ever

bool checkArmstrong(int n){

string s=to_string(n);

int size=s.size();

int num=0;

for(int i=0;i<s.size();i++)

{

int p=s[i]-'0';

num+=pow(p,size);

}

if(num==n) return true;

return false;

}

56 views
0 replies
2 upvotes

Interview problems

Why am i facing an errror

def checkArmstrong(n):

    dig=0

    dnum=n

    while dnum>0:

        dig+=1

        dnum=dnum//10

    armstrong=0

    dnum1=n

    while dnum1>0:

        armstrong+=((dnum1%10)**dig)

        dnum1=dnum1//10

    if armstrong==n:

        return("true")

    else:

        return("false")

59 views
0 replies
0 upvotes
Full screen
Console