Java Program to Check Armstrong Number or Not using For loop
Java program to check armstrong number or not using for loop. In this article, you will learn how to check armstrong number in java using for loop.
Example-1
Example-2
Armstrong Number
A positive integer is called an Armstrong number (of order n) if
You should have knowledge of the following topics in java programming to understand these programs:
- Java
java.util.Scanner
package - Java Math library
- Java
main()
method - Java
for
loop statement - Java
while
loop statement - Java
if-else
condition statement - Java
System.out.println()
function
Java Program to Check Armstrong Number or Not using For loop
// Java Program to Check Armstrong Number or Not using For loop
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x, y, rem, n = 0;
double r = 0.0;
// x - To store the input number
// y - To store the storage of original input number to check
// rem - To store the reminder
// r - To store the result variable
System.out.println("-----Enter the integer number-----");
x = in.nextInt();
y = x;
// store the number of digits of x in n
for (y = x; y != 0; ++n) {
y /= 10;
}
for (y = x; y != 0; y /= 10) {
rem = y % 10;
// store the sum of the power of individual digits in r
r += Math.pow(rem, n);
}
// if x is equal to r, the number is an Armstrong number
if ((int) r == x) {
System.out.println("\n" + x + " is an Armstrong number.");
} else {
System.out.println("\n" + x + " is not an Armstrong number.");
}
}
}
Output
-----Enter the integer number-----
407
407 is an Armstrong number.
Java Program to Check Armstrong Number or Not using While loop
// Java Program to Check Armstrong Number or Not using While loop
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x, y, z, rem, n = 0;
double r = 0.0;
// x - To store the input number
// y & z - To store the storage of original input number to check
// rem - To store the reminder
// r - To store the result variable
System.out.println("-----Enter the integer number-----");
x = in.nextInt();
z = y = x;
while (y != 0) {
y /= 10;
++n;
}
while (z != 0) {
rem = z % 10;
r += Math.pow(rem, n);
z /= 10;
}
// if x is equal to r, the number is an Armstrong number
if ((int) r == x) {
System.out.println("\n" + x + " is an Armstrong number.");
} else {
System.out.println("\n" + x + " is not an Armstrong number.");
}
}
}
Output
-----Enter the integer number-----
407
407 is an Armstrong number.
Java Program to Check Armstrong Number or Not without using Math.pow() function
// Java Program to Check Armstrong Number or Not without using Math.pow() function
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x, y, z, rem, rem2 = 1, n = 0, m = 0;
double r = 0.00;
// x - To store the input number
// y & z - To store the storage of original input number to check
// rem & rem2 - To store the reminder
// r - To store the result variable
System.out.println("-----Enter the integer number-----");
x = in.nextInt();
z = y = x;
while (y != 0) {
y /= 10;
++n;
}
m = n;
while (z != 0) {
rem = z % 10;
rem2 = 1;
while (n > 0) {
rem2 *= rem;
--n;
}
n = m;
r += rem2;
z /= 10;
}
// if x is equal to r, the number is an Armstrong number
if ((int) r == x) {
System.out.println("\n" + x + " is an Armstrong number.");
} else {
System.out.println("\n" + x + " is not an Armstrong number.");
}
}
}
Output
-----Enter the integer number-----
407
407 is an Armstrong number.
Explanation
In these given programs, we have taken input 407
a positive integer number to check it is an Armstrong or not using the for
loop and while
loop statement.
We applied the Armstrong number formula in these loops to check the sum of its digits' multiplication with its length is equal to the given input value.