-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirstDig.java
66 lines (52 loc) · 1.98 KB
/
FirstDig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Java program to find the first digit of the number
import java.util.Scanner;
public class FirstDig {
public static int firstnum(int n)
{
while(n > 10)
{
n = n / 10;
}
return n;
}
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.print("Enter the number :- ");
int n = s.nextInt();
System.out.println(firstnum(n));
}
}
/*
* The time complexity of the given Java program is O(log₁₀(n)).
Explanation:
The method firstnum(int n) reduces n by dividing it by 10 in each iteration of the while loop. This operation continues until n becomes less than or equal to 10,
effectively removing one digit from the number in each iteration.
The number of times you can divide a number n by 10 until it becomes a single digit is proportional to the number of digits in n. The number of digits in a number n
is approximately log₁₀(n).
Thus, the time complexity is O(log₁₀(n)), where n is the input number.
Space Complexity:
The space complexity is O(1) since no additional space is required except for a few variables used in the method.
*/
/*
* import java.util.Scanner;
public class FirstNum{
public static void main(String[] args)
{
System.out.print("Enter the number :- ");
Scanner s = new Scanner(System.in);
int n = s.nextInt();
System.out.print(FirstDigit(n));
}
public static int FirstDigit(int n)
{
double power = Math.log10(n);
int p = (int) power;
int a = (int) Math.pow(10,p);
int ans = n / a;
return ans;
}
}
The time complexity of the given Java code is O(1) (constant time). Since all the operations in the code are performed in constant time, the overall time complexity is O(1).
The space complexity is also O(1) since the program only uses a fixed amount of extra space (variables power, p, a, and ans), regardless of the size of the input.
*/