From e68334330ebc4a64d9d0b09b90c50b03c301c465 Mon Sep 17 00:00:00 2001 From: Utsav1999 Date: Sat, 25 Jul 2020 21:49:11 +0530 Subject: [PATCH 1/4] Added Count Digit program --- Maths/CountDigit.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Maths/CountDigit.java diff --git a/Maths/CountDigit.java b/Maths/CountDigit.java new file mode 100644 index 000000000000..2c3080e30d2b --- /dev/null +++ b/Maths/CountDigit.java @@ -0,0 +1,13 @@ +import java.util.*; +import java.lang.*; +// count the number of digits in a number +class CountDigit{ + public static void main(String args[]){ + Scanner sc = new Scanner(System.in); + System.out.print("Enter the number: "); + int number = sc.nextInt(); + int digits = 0; + digits = (int)Math.floor(Math.log10(number) + 1); + System.out.println("The number of digits present in the number: " + digits); + } +} \ No newline at end of file From ce5ca1738117b01ea28de2df8cf982ffbff9fa32 Mon Sep 17 00:00:00 2001 From: Utsav1999 Date: Sun, 26 Jul 2020 01:31:34 +0530 Subject: [PATCH 2/4] Update for zero and negative numbers --- Maths/CountDigit.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Maths/CountDigit.java b/Maths/CountDigit.java index 2c3080e30d2b..6610a35d3b18 100644 --- a/Maths/CountDigit.java +++ b/Maths/CountDigit.java @@ -7,7 +7,14 @@ public static void main(String args[]){ System.out.print("Enter the number: "); int number = sc.nextInt(); int digits = 0; - digits = (int)Math.floor(Math.log10(number) + 1); - System.out.println("The number of digits present in the number: " + digits); + if(number == 0) + { + System.out.println("The number of digits present in the number: 1"); + } + else + { + digits = (int)Math.floor(Math.log10(Math.abs(number)) + 1); + System.out.println("The number of digits present in the number: " + digits); + } } } \ No newline at end of file From da29549d6745704f3ba33bbb0d065e73c473af42 Mon Sep 17 00:00:00 2001 From: Utsav1999 Date: Sun, 26 Jul 2020 21:32:32 +0530 Subject: [PATCH 3/4] updated with Maths package --- Maths/CountDigit.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Maths/CountDigit.java b/Maths/CountDigit.java index 6610a35d3b18..fc63a0d6a159 100644 --- a/Maths/CountDigit.java +++ b/Maths/CountDigit.java @@ -1,8 +1,8 @@ import java.util.*; import java.lang.*; // count the number of digits in a number -class CountDigit{ - public static void main(String args[]){ +class CountDigit { + public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number: "); int number = sc.nextInt(); From 7f944d93dd2d5d7e90d291ebb668574e17530a6c Mon Sep 17 00:00:00 2001 From: Utsav1999 Date: Sun, 26 Jul 2020 21:36:19 +0530 Subject: [PATCH 4/4] updated with Maths package --- Maths/CountDigit.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/CountDigit.java b/Maths/CountDigit.java index fc63a0d6a159..8fdc385524a5 100644 --- a/Maths/CountDigit.java +++ b/Maths/CountDigit.java @@ -1,5 +1,5 @@ import java.util.*; -import java.lang.*; +package Maths; // count the number of digits in a number class CountDigit { public static void main(String args[]) {