From 0ce76208921237f66f1af506d4da575d1f067b45 Mon Sep 17 00:00:00 2001 From: shreeyachatzz <91721717+shreeyachatzz@users.noreply.github.com> Date: Sat, 2 Oct 2021 12:23:22 +0530 Subject: [PATCH] Create KeithNumber.java --- .../Java_Programs/Misc/KeithNumber.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 Program's_Contributed_By_Contributors/Java_Programs/Misc/KeithNumber.java diff --git a/Program's_Contributed_By_Contributors/Java_Programs/Misc/KeithNumber.java b/Program's_Contributed_By_Contributors/Java_Programs/Misc/KeithNumber.java new file mode 100644 index 0000000000..e3cd7eb2a7 --- /dev/null +++ b/Program's_Contributed_By_Contributors/Java_Programs/Misc/KeithNumber.java @@ -0,0 +1,75 @@ +//coded by shreeyachatzz +// Find whether a number is a Keith number +import java.util.Scanner; +public class KeithNumberChecker +{ + + public static void main()//Main function + { + Scanner scanner = new Scanner(System.in); + System.out.print("Please enter a number: "); + String n = scanner.nextLine(); + // Keith numbers must be > 10 + if (n.length() > 1 && isKeithNumber(n)) + { + System.out.println(n + " is a Keith number!"); + } + else + { + System.out.println(n + " is NOT a Keith number!"); + } + scanner.close(); + } + // Checks whether n number is a Keith number + public static boolean isKeithNumber(String n) + { + int b = n.length(); + // we keep only last n elements of the a + long[] a = new long[b]; + + for (int i = 0; i < b; i++) + { + a[i] = Long.valueOf(n.substring(i, i + 1)); + } + long next = 0; + long no = Long.valueOf(n); + + while (next < no) + { + next = 0; + for (int i = 0; i < b; i++) + { + next += a[i]; + if (i < b - 1) + { + a[i] = a[i + 1]; // shift a to left + } + else + { + a[i] = next; // add new value to the a + } + } + if (next == no) + { + return true; + } + } + return false; + } +} +/* +(1) +Please enter a number: 197 +197 is a Keith number! +*//* +(2) +Please enter a number: 12 +12 is NOT a Keith number! +*//* +(3) +Please enter a number: 14 +14 is a Keith number! +(4) +Please enter a number: 93993 +93993 is a Keith number! +*/