Coding Interview

coding interview questions part 1

Pinterest LinkedIn Tumblr

Armstrong Number

An Armstrong Number is a number that is the sum of its own digits each raised to the power of the number of digits
Armstrong numbers are those numbers in which sum of cube of all digits provides the same number.
Example:-
153
1^3 = 1
5^3 = 125
3^3 = 27
1^3 + 5^3 + 3^3
= 1 + 125 + 27
= 153

Source Code

package com.dsacode.Probelms;

public class ArmstrongNumer {

	public static boolean isArmStrong(int num) {
        int result = 0;
        int orig = num;
        
        while(num != 0){
            int remainder = num%10;
            result = result + remainder*remainder*remainder;
            num = num/10;
        }
        
        if(orig == result){
            return true;
        }
        
        return false;
    } 
	
	public static void main(String[] args) {
		
		if(isArmStrong(153))
			System.out.println("153 is an Armstrong Number! ");
		else
			System.out.println("153 is not an Armstrong Number! " );
		
		if(isArmStrong(12))
			System.out.println("12 is an Armstrong Number! ");
		else
			System.out.println("12 is not an Armstrong Number! " );

	}
}
#include "stdafx.h"

#include< iostream>

using namespace std;

bool isArmStrong(int num) {
	int result = 0;
	int orig = num;

	while (num != 0){
		int remainder = num % 10;
		result = result + remainder*remainder*remainder;
		num = num / 10;
	}

	if (orig == result){
		return true;
	}

	return false;
}

int _tmain(int argc, _TCHAR* argv[])
{
	if (isArmStrong(153))
		cout << "153 is an Armstrong Number! " << endl;
	else
		cout << "153 is not an Armstrong Number!" << endl;

	if (isArmStrong(12))
		cout << "12 is an Armstrong Number!" << endl;
	else
		cout << "12 is not an Armstrong Number! " << endl;

	
	return 0;
}

Output

153 is an Armstrong Number!
12 is not an Armstrong Number!

Algorithm Explanation

Take each digit from given number.
Take a cube of each digit and take summation.
Check whether the original number and sum of all digits are equal. If equal, the number is Armstrong number.
1 2 3 4 5

4 Comments

  1. Hey there I am so excited I found your weblog, I really found you by accident, while I was looking on Askjeeve for something else, Nonetheless I am here now and would just like to say many thanks for a fantastic post and a all round entertaining blog (I also love the theme/design), I don’t have time to read it all at the minute but I have book-marked it and also added your RSS feeds, so when I have time I will be back to read more, Please do keep up the awesome job.

  2. With havin so much content do you ever run into any issues of plagorism or copyright violation? My website has a lot of completely unique content I’ve either written myself or outsourced but it looks like a lot of it is popping it up all over the web without my authorization. Do you know any solutions to help reduce content from being ripped off? I’d truly appreciate it.

  3. I do believe all of the ideas you’ve presented in your post. They’re really convincing and can certainly work. Still, the posts are very short for beginners. May you please extend them a bit from next time? Thanks for the post.

Write A Comment