The source code to round off an integer number to the next lower multiple of 2 is given below. The given program is compiled and executed successfully.
// Java program to round off an integer number to
// the next lower multiple of 2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner SC = new Scanner(System.in);
int tmp = 1;
int num = 0;
int i = 0;
System.out.printf("Enter Number: ");
num = SC.nextInt();
if (num > 0) {
for (; tmp <= num >> 1;)
tmp = tmp << 1;
num = tmp;
} else {
num = ~num;
num = num + 1;
for (; tmp <= num >> 1;)
tmp = tmp << 1;
tmp = tmp << 1;
tmp = ~tmp;
tmp = tmp + 1;
num = tmp;
}
System.out.printf("Result is: %d\n", num);
}
}
Program/Source Code:
The source code to round off an integer number to the next lower multiple of 2 is given below. The given program is compiled and executed successfully.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer