You are reading the article Armstrong Number In Java Program Using For Loop updated in September 2023 on the website Speedmintonvn.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Armstrong Number In Java Program Using For Loop
What is Armstrong Number?In an Armstrong Number, the sum of power of individual digits is equal to number itself.
In other words the following equation will hold true
xy..z = xn + yn+.....+ znn is number of digits in number
For example this is a 3 digit Armstrong number
370 = 33 + 73 + o3 = 27 + 343 + 0 = 370Examples of Armstrong Numbers
0, 1, 4, 5, 9, 153, 371, 407, 8208, etc.Let’s write this in a program:
Java Program to check whether a number is Armstrong Number package com.guru99; public class ArmstrongNumber { public static void main(String[] args) { int inputArmstrongNumber = 153; 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); digitCubeSum = digitCubeSum + digit*digit*digit; System.out.println("Current digitCubeSum is "+digitCubeSum); tempNumber /= 10; } if(digitCubeSum == inputArmstrongNumber) System.out.println(inputArmstrongNumber + " is an Armstrong Number"); else System.out.println(inputArmstrongNumber + " is not an Armstrong Number"); } } Output 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 Program to Print Armstrong numbers from 0 to 999 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; digitCubeSum = digitCubeSum + digit * digit * digit; tempNumber /= 10; } if (digitCubeSum == inputArmstrongNumber) System.out.println(inputArmstrongNumber + " is an Armstrong Number"); } } } Output 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 NumberYou're reading Armstrong Number In Java Program Using For Loop
Update the detailed information about Armstrong Number In Java Program Using For Loop on the Speedmintonvn.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!