-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprog4.java
More file actions
57 lines (49 loc) · 1.07 KB
/
prog4.java
File metadata and controls
57 lines (49 loc) · 1.07 KB
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
/* (Level-Easy)
You are given a number N. You have to find the number of operations required to reach N from 0. You have 2 operations available:
Double the number
Add one to the number
Input:
The first line of input contains an integer N.
Output:
For each test case, in a new line, print the minimum number of operations required to reach N from 0.
Constraints:
1<=T<=100
1<=N<=104
Example:
Input:
8
Output:
4
Input:
7
Output:
5
Explanation:
Testcase1:
Input : N = 8
Output : 4
0 + 1 = 1, 1 + 1 = 2, 2 * 2 = 4, 4 * 2 = 8
Testcase2:
Input : N = 7
Output : 5
0 + 1 = 1, 1 + 1 = 2, 1 + 2 = 3, 3 * 2 = 6, 6 + 1 = 7
*/
import java.util.Scanner;
public class prog4 {
public static void main(String[] args) {
int count=0, i;
Scanner sc = new Scanner(System.in);
System.out.println("Inputs:");
int n = sc.nextInt();
System.out.println("Output:");
while(n>0){
if(n%2 != 0){
n=n-1;
}else{
n=n/2;
}
count=count+1;
}
System.out.println(count);
}
}