Cos'è il numero Armstrong?
In un numero Armstrong, la somma della potenza delle singole cifre è uguale al numero stesso.
In altre parole, la seguente equazione rimarrà vera
xy… z = xn + yn+… + zn
n è il numero di cifre nel numero
Ad esempio, questo è un numero Armstrong a 3 cifre
370 = 33 + 73 + o3= 27 + 343 + 0= 370
Esempi di numeri Armstrong
0, 1, 4, 5, 9, 153, 371, 407, 8208, etc.
Scriviamolo in un programma:
Programma Java per verificare se un numero è Armstrong Number
// ChecktempNumber è Armstrong o non utilizza il ciclo whilepacchetto com.guru99;public class ArmstrongNumber {public static void main (String [] args) {int inputArmstrongNumber = 153; // Immettere il numero per controllare armstrongint tempNumber, digit, digitCubeSum = 0;tempNumber = inputArmstrongNumber;while (tempNumber! = 0){/ * Ad ogni iterazione, il resto è alimentato dal numero di cifre n* /System.out.println ("Il numero corrente è" + tempNumber);cifra = tempNumber% 10;System.out.println ("La cifra corrente è" + cifra);// la somma dei cubi di ciascuna cifra è uguale al numero di tempo stessodigitCubeSum = digitCubeSum + cifra * cifra * cifra;System.out.println ("Current digitCubeSum è" + digitCubeSum);tempNumber / = 10;}// controlla giventempNumber e digitCubeSum è uguale o menoif (digitCubeSum == inputArmstrongNumber)System.out.println (inputArmstrongNumber + "è un numero Armstrong");altroSystem.out.println (inputArmstrongNumber + "non è un numero Armstrong");}}Produzione
Current Number is 153Current Digit is 3Current digitCubeSum is 27Current Number is 15Current Digit is 5Current digitCubeSum is 152Current Number is 1Current Digit is 1Current digitCubeSum is 153153 is an Armstrong Number
Programma Java per stampare numeri Armstrong da 0 a 999
// ChecktempNumber è Armstrong o non utilizza il ciclo whilepacchetto 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) {/ * Ad ogni iterazione, il resto è alimentato dal numero di cifre n* /cifra = tempNumber% 10;// la somma dei cubi di ciascuna cifra è uguale al numero di tempo stessodigitCubeSum = digitCubeSum + cifra * cifra * cifra;tempNumber / = 10;}// controlla giventempNumber e digitCubeSum è uguale o menoif (digitCubeSum == inputArmstrongNumber)System.out.println (inputArmstrongNumber + "è un numero Armstrong");}}}Produzione
0 is an Armstrong Number1 is an Armstrong Number153 is an Armstrong Number370 is an Armstrong Number371 is an Armstrong Number407 is an Armstrong Number