This Armstrong Number in Java using Scanner is a program that determines whether a given number is an ArmStrong number.
Armstrong Number is an integer such that the sum of its digit cubes equals the number itself.
Enter the integer to be checked as an input. To obtain the output, we now use modulus and division operations, as well as loops and if else statements.
Sample Code:
//Armstrong Number Program in Java using Scanner: Sample Code
package armstrong_number_in_java;
import java.util.Scanner;
public class Armstrong_Number_in_Java {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int n, count = 0, a, b, c, sum = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter any integer you want to check:");
n = s.nextInt();
a = n;
c = n;
while (a > 0) {
a = a / 10;
count++;
}
while (n > 0) {
b = n % 10;
sum = (int) (sum + Math.pow(b, count));
n = n / 10;
}
if (sum == c) {
System.out.println("Given number is Armstrong");
} else {
System.out.println("Given number is not Armstrong");
}
}
}
Output:
//Armstrong Number Program in Java using Scanner: Sample Code Output
Enter any integer you want to check:2
Given number is Armstrong
What is the importance of the Armstrong number in Java using a while loop?
Armstrong’s number is 371. First, the value of the given number (number) is stored in another integer variable, originalNumber.
This is due to the fact that we must compare the values of the final number and the original number at the end.
A while loop is then used to loop through originalNumber until it equals 0. System sample is importance for the programmer to train to solve simple problems.
Java Program for Armstrong Number using Scanner: Formula
An Armstrong number, also known as a narcissistic number, is a number that equals the sum of its own digits’ cubes.
370, for example, is an Armstrong number because it equals 333 + 777 + 000.
About the Project
Here is the system that checking if a given number is an Armstrong number. On a Windows system, the Java program is successfully compiled and run. The output of the program is also shown above the sample code.
Project Details and Technology
Project Name: | Armstrong Number in Java using Scanner with Source Code |
Abstract | The Armstrong Number in Java using Scanner is a basic system project that has the function of identifying the number if it is an Armstrong number or not. |
Language/s Used: | Java |
Java version (Recommended): | 8 |
Database: | N/A |
Type: | Desktop Application |
Developer: | Tedmar Enoria |
Updates: | 0 |
To start executing an Armstrong Number in Java using Scanner, make sure that you have NetBeans IDE or any platform code editor installed on your computer.
Java Armstrong number program: Procedure to run the system
Time needed: 3 minutes
- Step 1: Download Source Code
To get started, find the downloaded source code file below and click to start downloading it.
- Step 2: Extract File
Navigate to the file’s location when you’ve finished downloading it and right-click it to extract it.
- Step 3: Run the project
Next, open NetBeans IDE click open project, and choose your download.
- Step 4: Run the project.
Next, right-click the project folder and click run.
Download the Source Code below
Summary
For those Web developers interested in learning more about web apps, this system is a fully functional project.
Related Articles
- Number Pattern Program in Java using While Loop
- Java Program to Calculate Average of n Numbers Source Code
- Java Program to Calculate Electricity Bill with Source Code
- Factorial Program in Java using Recursion Source Code
Inquiries
If you have any questions or comments on the project, please leave a comment below.