armstrong numbers problem java code example

Example 1: Java program to print all armstrong numbers between given range

// 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("Please enter starting number range: ");
      startNumber = sc.nextInt();
      System.out.println("Please enter ending number range: ");
      endNumber = sc.nextInt();
      for(a = startNumber + 1; a < 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("Armstrong numbers between given range " + startNumber + " and " + endNumber + ": ");
            }
            System.out.print(a + "  ");
            count++;
         }
      }
      // if there is no Armstrong number found between range
      if(count == 0)
      {
         System.out.println("Sorry!! There's no armstrong number between given range " + startNumber + " and " + endNumber);
      }
      sc.close();
   }
}

Example 2: 4 digit armstrong number in java

// 4 digit armstrong number in java
public class ArmstrongNumberDemo
{
   public static void main(String[] args) 
   {
      int num = 9474, realNumber, remainder, output = 0, a = 0;
      realNumber = num;
      for(;realNumber != 0; realNumber /= 10, ++a);
      realNumber = num;
      for(;realNumber != 0; realNumber /= 10)
      {
         remainder = realNumber % 10;
         output += Math.pow(remainder, a);
      }
      if(output == num)
      {
         System.out.println(num + " is an Armstrong number.");
      }
      else
      {
         System.out.println(num + " is not an Armstrong number.");
      }
   }
}

Example 3: armstrong number in java

int c=0,a,temp;  
    int n=153;//It is the number to check armstrong  
    temp=n;  
    while(n>0)  
    {  
    a=n%10;  
    n=n/10;  
    c=c+(a*a*a);  
    }  
    if(temp==c)  
    System.out.println("armstrong number");   
    else  
        System.out.println("Not armstrong number");

Example 4: armstrong numbers problem java

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("Enter the number: ");
		int input = in.nextInt(); //1634
		String val = String.valueOf(input);
		char[] charArray = val.toCharArray();  //charArray[0] = "1" , charArray[1] = "6", charArray[2] = "3", charArray[3] = "4"
		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 "cubedNumArray" array.
		for(int i=0; i<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("Entered number is an Armstrong number.");
		}else {
			System.out.println("Entered number is NOT an Armstrong number.");
		}
		
		in.close();
	}

  	//Calculate & 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);
	}
}

Tags:

Misc Example