From 63c6393a3ee651036cfdea159eaa6423db7d6056 Mon Sep 17 00:00:00 2001 From: Sayan Biswas Date: Sun, 15 Oct 2023 11:16:10 +0530 Subject: [PATCH] Create ArmstrongNumber.java --- Basics/ArmstrongNumber.java | 79 +++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 Basics/ArmstrongNumber.java diff --git a/Basics/ArmstrongNumber.java b/Basics/ArmstrongNumber.java new file mode 100644 index 0000000..d9cd717 --- /dev/null +++ b/Basics/ArmstrongNumber.java @@ -0,0 +1,79 @@ +package com.sbiswas001.twelveproject; + +import java.util.Scanner; + +/** + * Checks if a number is armstrong or not. + * A number is armstrong if the sum of each digit to the power + * number of digits is equal to the number itself. + * @author Sayan Biswas + * @version 25.04.2022 + */ +public class ArmstrongNumber { + /** + * Stores the number + */ + private int number; + + /** + * Initializes instance variables + */ + private ArmstrongNumber() { + number = 0; + } + + /** + * Inputs a number from user + */ + private void input() { + Scanner sc = new Scanner(System.in); + System.out.print("Enter number: "); + number = Integer.parseInt(sc.nextLine()); + } + + /** + * Returns number of digits of a number + * @param n Number to be checked + * @return Number of digits + */ + private int numberOfDigits(int n) { + return (int)Math.floor(Math.log10(n))+1; + } + + /** + * Checks if a number is armstrong or not. + * A number is armstrong if the sum of each digit to the + * power number of digits is equal to the number itself. + * @param a Number to be checked + * @return true or false + */ + private boolean armstrongCheck(int a) { + int sum = 0; + while(a > 0) { + sum += (int)Math.pow(a % 10, + numberOfDigits(number)); + a /= 10; + } + return sum == number; + } + + /** + * Displays if number is armstrong or not + */ + private void display() { + + System.out.println(armstrongCheck(number) ? + "Number is armstrong." : + "Number is not armstrong."); + } + + /** + * Calls other methods + * @param args Arguments passed to main method + */ + public static void main(String[] args) { + ArmstrongNumber ob = new ArmstrongNumber(); + ob.input(); + ob.display(); + } +}