Menu
Topics Index
...
`

Print Amstrong Numbers In A Given Range - Java Example Program


Print Amstrong Numbers In A Given Range
class PrintAmstrongNumbersInAGivenRange
{
    public static void main(String args[])
    {
        int min = 1, max = 500, temp, sum, input;
            
        System.out.println("Amstrong numbers in a given range are:");
            
            for(input = min; input < max; input++)
            {
                temp = input;
                sum = 0;
                printAmstrongNumbers(temp, input);
                    
            }
            private static void printAmstrongNumbers(int temp,  int input)
            {
                int mod;
                int sum = 0;
                while(temp != 0)
                {
                    mod = temp % 10;
                    temp = temp / 10;
                    sum = sum + (mod * mod * mod);
                
                
                }
                if(sum == input)
                {
                    System.out.print(input+" ");
                    
                }
        }
}
OUTPUT

Amstrong numbers in a given range are:
1 153 370 371 407

DESCRIPTION

THINGS TO TRY
  • Try to print the amstrong numbers between 10 to 20. The output should be empty since there is no amstrong numbers between 10 to 20.

© meritcampus 2019

All Rights Reserved.

Open In App