Answers for "Java program to print all armstrong numbers between given range"

3

Java program to print armstrong number from 1 to 1000

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;
      }
   }
}
Posted by: Guest on February-09-2021
3

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();
   }
}
Posted by: Guest on February-22-2021
0

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);
	}
}
Posted by: Guest on May-27-2021

Code answers related to "Java program to print all armstrong numbers between given range"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language