Update OOP/Java/Lab/Week3/Armstrong.java

This commit is contained in:
Aadit Agrawal 2024-08-31 01:22:13 +05:30
parent f44fd64fd0
commit f8aace509b

View File

@ -1,37 +1,26 @@
import java.util.Scanner;
import java.lang.Math;
class Armstrong{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int a,b,digit;
int digisum=0;
int c=0;
System.out.println("Enter the number you went to check for Armstrong-ness:");
a = sc.nextInt();
b = a;
while(b!=0){
c++;
b/=10;
}
b = a;
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{
System.out.println("The number is not an armstrong number.");
}
}
}
import java.util.Scanner;
class Armstrong {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();
int b = String.valueOf(n).length();
int sum = 0;
int temp = n;
while (temp > 0) {
int r = temp % 10;
sum += Math.pow(r, b);
temp /= 10;
}
if (sum == n) {
System.out.println("The given number is an Armstrong Number.");
} else {
System.out.println("The given number is not an Armstrong Number.");
}
sc.close();
}
}