java program to print all armstrong numbers between given range

Solutions on MaxInterview for java program to print all armstrong numbers between given range by the best coders in the world

showing results for - "java program to print all armstrong numbers between given range"
Regina
01 Jan 2017
1public class ArmstrongBetween1To1000
2{
3   public static void main(String[] args)
4   {
5      int number, n, total = 0;
6      System.out.println("Armstrong number between 1 to 1000: ");
7      for(int a = 1; a <= 1000; a++)
8      {
9         number = a;
10         while(number > 0)
11         {
12            n = number % 10;
13            total = total + (n * n * n);
14            number = number / 10;
15         }
16         if(total == a)
17         {
18            System.out.print(a + " ");
19         }
20         total = 0;
21      }
22   }
23}
Kyron
28 Apr 2017
1// Java program to print all armstrong numbers between given range
2import java.util.Scanner;
3public class ArmstrongNumbersGivenRange 
4{
5   public static void main(String[] args) 
6   {
7      int number, startNumber, endNumber, a, rem, n, count = 0;
8      Scanner sc = new Scanner(System.in);
9      System.out.println("Please enter starting number range: ");
10      startNumber = sc.nextInt();
11      System.out.println("Please enter ending number range: ");
12      endNumber = sc.nextInt();
13      for(a = startNumber + 1; a < endNumber; a++)
14      {
15         n = a;
16         number = 0;
17         while(n != 0)
18         {
19            rem = n % 10;
20            number = number + rem * rem * rem;
21            n = n / 10;
22         }
23         if(a == number)
24         {
25            if(count == 0)
26            {
27               System.out.println("Armstrong numbers between given range " + startNumber + " and " + endNumber + ": ");
28            }
29            System.out.print(a + "  ");
30            count++;
31         }
32      }
33      // if there is no Armstrong number found between range
34      if(count == 0)
35      {
36         System.out.println("Sorry!! There's no armstrong number between given range " + startNumber + " and " + endNumber);
37      }
38      sc.close();
39   }
40}
Isiah
01 May 2018
1import java.util.Scanner;
2
3/*
4 *@author: Mayank Manoj Raicha
5 * Armstrong Number in Java: A positive number is called armstrong number if it is equal to the sum of cubes of its digits 
6 * for example 0, 1, 153, 370, 371, 407 etc.
7 * 153 = (1*1*1)+(5*5*5)+(3*3*3)  = 1+125+27 = 153
8 */
9public class ArmstrongNumberExample {
10
11	public static void main(String[] args) {
12		
13		int sum = 0;
14		
15		Scanner in = new Scanner(System.in);
16		System.out.println("Enter the number: ");
17		int input = in.nextInt(); //1634
18		String val = String.valueOf(input);
19		char[] charArray = val.toCharArray();  //charArray[0] = "1" , charArray[1] = "6", charArray[2] = "3", charArray[3] = "4"
20		int[] numArray = new int[charArray.length]; //Declaring this array to store the result of getPowerOfNumber() method for each digit.
21		
22		//for each char element calculate the power of number and store it in the "cubedNumArray" array.
23		for(int i=0; i<charArray.length; i++) {
24			numArray[i] = getPowerOfNumber(Integer.parseInt(String.valueOf(charArray[i])), charArray.length);
25			sum = sum + numArray[i];
26		}
27		
28      //Compare if the resulting sum is equal to the original input.
29		if(sum == input) {
30			System.out.println("Entered number is an Armstrong number.");
31		}else {
32			System.out.println("Entered number is NOT an Armstrong number.");
33		}
34		
35		in.close();
36	}
37
38  	//Calculate & Return the value of the first argument raised to the power of the second argument
39	public static int getPowerOfNumber(int num, int count) {
40		return (int) Math.pow(num, count);
41	}
42}
queries leading to this page
java armstrong numberjava program to find armstrong numbersarmstrong number logic in javaarmstrong no in javaarmstrong number in a given range injavaprinting the armstrong numbers between the given intervals in javaprint all armstrong numbers between two intervals javaarmstrong number in java using for loopwhat is armstrong number in javaprint armstrong number between 1 to n using javaarmstrong number in range javaarmstrong number from 1 to 1000 in javajava program to check armstrong numberhow to find an armstrong number in javadisplay all the armstrong number in a given range in javaarmstrong number in javaarmstrong number program in javaarmstrong number or not in javajava program to print all armstrong numbers between given rangejava program for armstrong number between 1 to 1000print out all armstrong number in java4 digit armstrong number in javajava program for armstrong numberwrite a java program to print armstrong number in a given interval rangearmstrong number in a given range in javawrite a program to print all armstrong numbers in a given range in javaarmstrong numbers program javaarmstrong number javaarmstrong number in java given rangearmstrong numbers problem javajava program to print armstrong number from 1 to 1000armstrong no javajava program to print armstrong numbers between given rangefind armstrong number within a range javaarmstrong code javaarmstrong in range javaprogram of armstrong number in javaarmstrong no logic in javaarmstrong number in java geeksforgeeksarmstrong number between given range in javausing java generate armstrong no within range and display sum also printing the armstrong numbers between the given interval in javahow to find the armstrong number in javaarmstrong number in java for 4 digitsjava program to display armstrong numbers within a range javaprogram for armstrong numbers in javajava program to print all armstrong numbers between given range