Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.knowledgeblackbelt.students.ideynega.exercises;
- public class Exercise284 {
- private static long getBase2DecimalRepresentation(int num) {
- long result = 0;
- long mult = 1;
- while(num / 2 > 0) {
- result += num % 2 * mult;
- mult *= 10;
- num /= 2;
- }
- if(num % 2 == 1) {
- result += mult;
- }
- return result;
- }
- public static void main(String[] args) {
- System.out.println(getBase2DecimalRepresentation(8));
- }
- }
- /*
- Write a method that receives a small integer in base 10 and prints out the original number represented in base 2.
- For example, giving 8 will print out "1000".
- */
Add Comment
Please, Sign In to add comment