Armstrong-Nummer im JAVA-Programm mit For-Schleife
Was ist die Armstrong-Zahl?
In einer Armstrong-Zahl ist die Potenzsumme der einzelnen Ziffern gleich der Zahl selbst.
Mit anderen Worten gilt die folgende Gleichung
xy..z = xn + yn+.....+ zn
n ist die Anzahl der Stellen in Zahl
Dies ist beispielsweise eine dreistellige Armstrong-Nummer
370 = 33 + 73 + o3 = 27 + 343 + 0 = 370
Beispiele für Armstrong-Zahlen
0, 1, 4, 5, 9, 153, 371, 407, 8208, etc.
Lassen Sie uns dies in einem Programm schreiben:
Java-Programm zum Prüfen, ob eine Nummer eine Armstrong-Nummer ist
//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"); } }
Ausgabe
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
Java-Programm zum Drucken von Armstrong-Zahlen von 0 bis 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"); } } }
Ausgabe
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
- Java-Hello-World-Programm
- Java for-each-Schleife
- Konstruktorüberladung in Java:Was ist &Programmbeispiele
- Java-Programm zum Überprüfen der Primzahl
- Programm zum Drucken von Primzahlen von 1 bis 100 in Java
- Fibonacci-Reihe in Java mit Rekursions- und Schleifenprogramm
- So kehren Sie eine Zeichenfolge in Java mithilfe von Rekursion um
- Palindrom-Zahlenprogramm in Java mit While- und For-Schleife
- Insertion Sort Algorithmus in Java mit Programmbeispiel
- Auswahlsortierung im Java-Programm mit Beispiel