Skip to content

Instantly share code, notes, and snippets.

@0ryant
Created August 1, 2019 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save 0ryant/5d6d0809df8e9ec9234a7c9f50264a98 to your computer and use it in GitHub Desktop.
Save 0ryant/5d6d0809df8e9ec9234a7c9f50264a98 to your computer and use it in GitHub Desktop.
Java - Coding Challenge 2 - MegaBytes Converter
public class MegaBytesConverter {
public static void printMegaBytesAndKiloBytes(int kiloBytes){
if (kiloBytes < 0){
System.out.println("Invalid Value");
} else {
int megabytes = (kiloBytes/1024);
int kiloRemainder = kiloBytes%1024;
System.out.println(kiloBytes+" KB = "+megabytes+" MB and "+kiloRemainder+" KB");
}
}
}
@johnkamara
Copy link

excellent work with your code, I used it and its works
please mine although with an output but not required under the Java check solution course:

public class MegaBytesConverter {

public static long printMegaBytesAndKiloBytes(double bitsPerKilobytes) {
    if (bitsPerKilobytes <0) {
        return -1;
    }
    return Math.round(bitsPerKilobytes % 1024);
}

public static void printConversion(double bitsPerKilobytes) {
    if (bitsPerKilobytes <0) {
        System.out.println("Invalid Va;ue");
    } else {
        long bitsPerBytes = printMegaBytesAndKiloBytes(bitsPerKilobytes);
        System.out.println(bitsPerKilobytes +
                " KB = " + bitsPerBytes +
                " KB");
}

@0ryant
Copy link
Author

0ryant commented Mar 11, 2021

great stuff :)

@aiham-lab
Copy link

public class Main {
public static void main(String[] args) {

}

public static void printMegaBytesAndkiloBytes(int kiloBytes) {
    if (kiloBytes < 0) {
        System.out.println("Invalid value");
    } else {
        System.out.println(kiloBytes + "KB  " + kiloBytes / 1024 + "MB   " + kiloBytes % 1024 + "KB");
    }
}

}

@travelonether
Copy link

Nice selection of code for both IntelliJ and the Java checker ! Perfect.

@Suraj7887
Copy link

So good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment