From f8aace509b0cd9f7282325da49040520c653cc60 Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Sat, 31 Aug 2024 01:22:13 +0530 Subject: [PATCH] Update OOP/Java/Lab/Week3/Armstrong.java --- OOP/Java/Lab/Week3/Armstrong.java | 63 +++++++++++++------------------ 1 file changed, 26 insertions(+), 37 deletions(-) diff --git a/OOP/Java/Lab/Week3/Armstrong.java b/OOP/Java/Lab/Week3/Armstrong.java index 9f49448..7f8837b 100644 --- a/OOP/Java/Lab/Week3/Armstrong.java +++ b/OOP/Java/Lab/Week3/Armstrong.java @@ -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."); - } - - } -} \ No newline at end of file +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(); + } +}