armstrong code example

Example 1: armstrong

public class Armstrong {

    public static void main(String[] args) {

        int number = 1634, originalNumber, remainder, result = 0, n = 0;

        originalNumber = number;

        for (;originalNumber != 0; originalNumber /= 10, ++n);

        originalNumber = number;

        for (;originalNumber != 0; originalNumber /= 10)
        {
            remainder = originalNumber % 10;
            result += Math.pow(remainder, n);
        }

        if(result == number)
            System.out.println(number + " is an Armstrong number.");
        else
            System.out.println(number + " is not an Armstrong number.");
    }
}

Example 2: armstrong

//examples = 370;371;153;407;
// made by Kashish Vaid the great
#include <stdio.h>
#include <math.h>
void main()
{
int i, num, result=0, remainder;
// applicable for 3 digits only 
printf("Enter 3 digit number: ");
scanf("%d",&num);

for( i=num ; i != 0 ; i/=10)
		{ 
  		remainder = i % 10;
		result += remainder * remainder * remainder;
        }
    // if else shortcut
    ( (result == num) ? printf("%d is an Armstrong no.",num) : printf("%d isn't an Armstrong no.",num) );
}
// made by Kashish Vaid the great

Tags:

Cpp Example