Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accepts input from users instead of using a hardcoded value, handles… #189

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions NumberOfDigit.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,26 @@
this algorithm has a time complexity of O(n)
we can simply calculate it by log(number)+1.
here is the code for it :- */
import java.util.*;
import java.util.Scanner;

class NumberOfDigit
{
public static void main(String args[])
{
int num=356, digits=0;
digits = (int)Math.log10(num) + 1;
System.out.println(digits);
}
class NumberOfDigits{
public static void main(String[] args)
{
Scanner sc = new Scaneer(System.in);

System.out.print("Enter a number");
int num = sc.nextInt();

if (num == 0)
System.out.print("Number of digits: 1");

else
{
int digits = (int)Math.log10(Math.abs(num))+1;

System.out.println("Number of digits: " + digits);
}
sc.close();
}
}