Q:

Write a Java Program to print Armstrong numbers between desired Range

belongs to collection: Java Basic Solved Programs

0

Print Armstrong Numbers:

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

Let’s look at the following Java program.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

SOURCE CODE ::

import java.util.Scanner;
import java.lang.Math;
public class Armstrong1
{
    public static void main(String args[])
    {
        int num1, num2, i, n, rem, temp, count=0,a;
        Scanner scan = new Scanner(System.in);
        
        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; i<num2; i++)
        {
            temp = i;
            n = 0;
            a=countdigit(temp);
            while(temp!=0)
            {
                rem = temp%10;
                temp = temp/10;
                 n = (int) (n + Math.pow(rem,a)) ;

            }
            if(i == n && i>10) //armstrong no.s greater than 1 digit number...
            {
                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.");
        }
    }
    
    //function to count no of digits in each iteration of number.....
    public static int countdigit(int n)
    {
      int c=0;
      
      while(n>0)
      {
          n=n/10;
          
          c++;
      }
      return c;
      
    }
}

OUTPUT ::

Enter the Interval :
Enter Starting Number : 100
Enter Ending Number : 10000

Armstrong Numbers Between the Given Interval are :
153  370  371  407  1634  8208  9474

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Solved Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a Java Program to Print Floyd’s Triangle u... >>
<< Write a Java Program to display n Prime Numbers us...