Manufaturação industrial
Internet das coisas industrial | Materiais industriais | Manutenção e reparo de equipamentos | Programação industrial |
home  MfgRobots >> Manufaturação industrial >  >> Industrial programming >> Java

Armstrong Number no programa JAVA usando For Loop

O que é o número Armstrong?


Em um número de Armstrong, a soma da potência dos dígitos individuais é igual ao próprio número.

Em outras palavras, a seguinte equação será verdadeira
xy..z = xn + yn+.....+ zn

n é o número de dígitos em número

Por exemplo, este é um número Armstrong de 3 dígitos
370 = 33 + 73 + o3
 = 27 + 343 + 0
 = 370

Exemplos de números de Armstrong
 0, 1, 4, 5, 9, 153, 371, 407, 8208, etc.

Vamos escrever isso em um programa:

Programa Java para verificar se um número é Armstrong Number

//ChecktempNumber is Armstrong or not using while loop
package com.guru99;
 
public class ArmstrongNumber {
 
	public static void main(String[] args) {
		
		 int inputArmstrongNumber = 153; //Input number to check armstrong  
		 int tempNumber, digit, digitCubeSum = 0;
 
	       tempNumber = inputArmstrongNumber;
	        while (tempNumber != 0)
	        {
	        	
	        	/* On each iteration, remainder is powered by thetempNumber of digits n
	        	 */
	            System.out.println("Current Number is "+tempNumber);
	            digit =tempNumber % 10;
				System.out.println("Current Digit is "+digit);
	            //sum of cubes of each digits is equal to thetempNumber itself
	            digitCubeSum = digitCubeSum + digit*digit*digit;
				System.out.println("Current digitCubeSum is "+digitCubeSum);
	            tempNumber /= 10;
	           
	        }
 
	        //check giventempNumber and digitCubeSum is equal to or not 
	        if(digitCubeSum == inputArmstrongNumber)
	            System.out.println(inputArmstrongNumber + " is an Armstrong Number");
	        else
	            System.out.println(inputArmstrongNumber + " is not an Armstrong Number");
 
	}
 
}

Saída

Current Number is 153
Current Digit is 3
Current digitCubeSum is 27
Current Number is 15
Current Digit is 5
Current digitCubeSum is 152
Current Number is 1
Current Digit is 1
Current digitCubeSum is 153
153 is an Armstrong Number

Programa Java para imprimir números Armstrong de 0 a 999

//ChecktempNumber is Armstrong or not using while loop
package com.guru99;

public class ArmstrongNumber {

    public static void main(String[] args) {
        int tempNumber, digit, digitCubeSum;

        for (int inputArmstrongNumber = 0; inputArmstrongNumber < 1000; inputArmstrongNumber++) {
            tempNumber = inputArmstrongNumber;
            digitCubeSum = 0;
            while (tempNumber != 0) {

                /* On each iteration, remainder is powered by thetempNumber of digits n
                 */

                digit = tempNumber % 10;

                //sum of cubes of each digits is equal to thetempNumber itself
                digitCubeSum = digitCubeSum + digit * digit * digit;

                tempNumber /= 10;

            }

            //check giventempNumber and digitCubeSum is equal to or not 
            if (digitCubeSum == inputArmstrongNumber)
                System.out.println(inputArmstrongNumber + " is an Armstrong Number");

        }

    }

}

Saída

0 is an Armstrong Number
1 is an Armstrong Number
153 is an Armstrong Number
370 is an Armstrong Number
371 is an Armstrong Number
407 is an Armstrong Number

Java

  1. Programa Java Hello World
  2. Java para cada loop
  3. Sobrecarga de construtor em Java:o que é e exemplos de programas
  4. Programa Java para verificar o número primo
  5. Programa para imprimir números primos de 1 a 100 em Java
  6. Série Fibonacci em Java usando o programa de recursão e loops
  7. Como reverter uma string em Java usando recursão
  8. Programa Palindrome Number em Java usando while &for Loop
  9. Algoritmo de ordenação por inserção em Java com exemplo de programa
  10. Classificação de seleção no programa Java com exemplo