Update OOP/Java/Lab/Week3/Armstrong.java
This commit is contained in:
parent
f44fd64fd0
commit
f8aace509b
@ -1,37 +1,26 @@
|
|||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
import java.lang.Math;
|
|
||||||
|
|
||||||
class Armstrong {
|
class Armstrong {
|
||||||
public static void main(String args[]){
|
|
||||||
|
public static void main(String[] args) {
|
||||||
Scanner sc = new Scanner(System.in);
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
int a,b,digit;
|
System.out.println("Enter a number");
|
||||||
int digisum=0;
|
|
||||||
int c=0;
|
|
||||||
|
|
||||||
System.out.println("Enter the number you went to check for Armstrong-ness:");
|
int n = sc.nextInt();
|
||||||
a = sc.nextInt();
|
int b = String.valueOf(n).length();
|
||||||
|
int sum = 0;
|
||||||
b = a;
|
int temp = n;
|
||||||
|
while (temp > 0) {
|
||||||
while(b!=0){
|
int r = temp % 10;
|
||||||
c++;
|
sum += Math.pow(r, b);
|
||||||
b/=10;
|
temp /= 10;
|
||||||
}
|
}
|
||||||
|
if (sum == n) {
|
||||||
b = a;
|
System.out.println("The given number is an Armstrong Number.");
|
||||||
|
|
||||||
while(b!=0){
|
|
||||||
digit = b%10;
|
|
||||||
digisum += Math.pow(digit, c);
|
|
||||||
b /= 10;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(digisum == a){
|
|
||||||
System.out.println("The number is an armstrong number.");
|
|
||||||
} else {
|
} else {
|
||||||
System.out.println("The number is not an armstrong number.");
|
System.out.println("The given number is not an Armstrong Number.");
|
||||||
}
|
}
|
||||||
|
sc.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user