Java program to print all armstrong numbers between given range

public class ArmstrongBetween1To1000
{
   public static void main(String[] args)
   {
      int number, n, total = 0;
      System.out.println("Armstrong number between 1 to 1000: ");
      for(int a = 1; a <= 1000; a++)
      {
         number = a;
         while(number > 0)
         {
            n = number % 10;
            total = total + (n * n * n);
            number = number / 10;
         }
         if(total == a)
         {
            System.out.print(a + " ");
         }
         total = 0;
      }
   }
}

4.5
2

                                    import java.util.Scanner;

/*
 *@author: Mayank Manoj Raicha
 * Armstrong Number in Java: A positive number is called armstrong number if it is equal to the sum of cubes of its digits 
 * for example 0, 1, 153, 370, 371, 407 etc.
 * 153 = (1*1*1)+(5*5*5)+(3*3*3)  = 1+125+27 = 153
 */
public class ArmstrongNumberExample {

	public static void main(String[] args) {
		
		int sum = 0;
		
		Scanner in = new Scanner(System.in);
		System.out.println(&quot;Enter the number: &quot;);
		int input = in.nextInt(); //1634
		String val = String.valueOf(input);
		char[] charArray = val.toCharArray();  //charArray[0] = &quot;1&quot; , charArray[1] = &quot;6&quot;, charArray[2] = &quot;3&quot;, charArray[3] = &quot;4&quot;
		int[] numArray = new int[charArray.length]; //Declaring this array to store the result of getPowerOfNumber() method for each digit.
		
		//for each char element calculate the power of number and store it in the &quot;cubedNumArray&quot; array.
		for(int i=0; i&lt;charArray.length; i++) {
			numArray[i] = getPowerOfNumber(Integer.parseInt(String.valueOf(charArray[i])), charArray.length);
			sum = sum + numArray[i];
		}
		
      //Compare if the resulting sum is equal to the original input.
		if(sum == input) {
			System.out.println(&quot;Entered number is an Armstrong number.&quot;);
		}else {
			System.out.println(&quot;Entered number is NOT an Armstrong number.&quot;);
		}
		
		in.close();
	}

  	//Calculate &amp; Return the value of the first argument raised to the power of the second argument
	public static int getPowerOfNumber(int num, int count) {
		return (int) Math.pow(num, count);
	}
}

4.5 (2 Votes)
0
4
5
Undying_War 100 points

                                    // Java program to print all armstrong numbers between given range
import java.util.Scanner;
public class ArmstrongNumbersGivenRange 
{
   public static void main(String[] args) 
   {
      int number, startNumber, endNumber, a, rem, n, count = 0;
      Scanner sc = new Scanner(System.in);
      System.out.println(&quot;Please enter starting number range: &quot;);
      startNumber = sc.nextInt();
      System.out.println(&quot;Please enter ending number range: &quot;);
      endNumber = sc.nextInt();
      for(a = startNumber + 1; a &lt; endNumber; a++)
      {
         n = a;
         number = 0;
         while(n != 0)
         {
            rem = n % 10;
            number = number + rem * rem * rem;
            n = n / 10;
         }
         if(a == number)
         {
            if(count == 0)
            {
               System.out.println(&quot;Armstrong numbers between given range &quot; + startNumber + &quot; and &quot; + endNumber + &quot;: &quot;);
            }
            System.out.print(a + &quot;  &quot;);
            count++;
         }
      }
      // if there is no Armstrong number found between range
      if(count == 0)
      {
         System.out.println(&quot;Sorry!! There's no armstrong number between given range &quot; + startNumber + &quot; and &quot; + endNumber);
      }
      sc.close();
   }
}

4 (5 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source