codescracker


java

Java Program to Generate Armstrong Numbers



« Previous Program Next Program »


Generate Armstrong Numbers

To generate Armstrong number in Java Programming, you have to ask to the user to enter the interval in which he/she want to generate Armstrong numbers between desired range as shown in the following program.

Java Programming Code to Generate Armstrong Numbers

Following Java Program ask to the user to enter the interval, to print the Armstrong numbers in that interval. Since if you start from 1 then first Armstrong number will be 153. So enter the interval in which 153 is in, like enter starting number as 1, 2, 3, 4......etc. but you have to enter the ending number which is greater than 154 like 154, 155, 156.......etc. It is just a clue that first Armstrong number is 153 so to print Armstrong numbers, you must have to enter the ending number which is greater than 154. Let's look at the following Java program.

/* Java Program Example - Generate Armstrong Numbers */
		
import java.util.Scanner;

public class JavaProgram
{
    public static void main(String args[])
    {
        int num1, num2, i, n, rem, temp, count=0;
        Scanner scan = new Scanner(System.in);
        
        /* enter the interval (two number) */
        
        System.out.print("Enter the Interval :\n");
		
        System.out.print("Enter Starting Number : ");
        num1 = scan.nextInt();
        System.out.print("Enter Ending Number : ");
        num2 = scan.nextInt();
		
        for(i=num1+1; i<num2; i++)
        {
            temp = i;
            n = 0;
            while(temp != 0)
            {
                rem = temp%10;
                n = n + rem*rem*rem;
                temp = temp/10;
            }
            if(i == n)
            {
                if(count == 0)
                {
                    System.out.print("Armstrong Numbers Between the Given Interval are :\n");
                }
                System.out.print(i + "  ");
                count++;
            }
        }
        if(count == 0)
        {
            System.out.print("Armstrong Number not Found between the Given Interval.");
        }
    }
}

When the above Java Program is compile and executed, it will produce the following output. Above Java Programming Example Output (for Armstrong numbers found):

Java Program generate armstrong numbers

Above Java Programming Example Output (for Armstrong numbers not found):

Java Program print armstrong numbers

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test