programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Jeanne Boyarsky
• Liutauras Vilda
Sheriffs:
• Rob Spoor
• Bear Bibeault
• Tim Cooke
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Piet Souris
Bartenders:
• Frits Walraven
• Himai Minh

# Can someone help me with my homework. First year CS student.

Greenhorn
Posts: 3
• Number of slices to send:
Optional 'thank-you' note:
So i am supposed to write a program where you can enter any number of bytes in the input and it is supposed to tell you how many gigabytes, megabytes, kilobytes, and bytes you just entered was. I will post down below what I have so far but I am stumbled. I know somehow you can use the Math.pow functions since KB is 2^10th bytes, and MB is 2^20 bytes, and GB is 2^30 bytes. I am just unsure on how to implement them. Thanks for your help!

Bartender
Posts: 2856
10
• Number of slices to send:
Optional 'thank-you' note:
Hi and welcome to Coderanch

While posting code, make sure to UseCodeTags, it makes code readable and easier to understand.
Now coming to your problem, are you required to mention the bytes in GB, MB and KB or just the nearest unit.
Either case, you should not require the use of Math.pow(). Simply dividing the number by 1024 repeatedly gives you the required unit.
And since you are using division, you are better off using doubles instead of ints.

For example 898745489 bytes is 877681.1416 KB, 857.11049 MB or 0.83702 GB.

Marshal
Posts: 72993
330
• Number of slices to send:
Optional 'thank-you' note:
There is a way of getting KiB, MiB, GiB and TiB as integers by using the division and remainder operators together. You can read about those operators in the Java® Language Specification, but that can be difficult to understand. If you want GiB you are better off with a long than with an int, to give you enough capacity for all the digits.
There is another way to get those figures with the bitshift and bitwise AND operators, but that is an advanced topic.

By the way: there are two measurement types. There are kB. MB, GB and TB which go up in ratios of 1000, and there are KiB MiB GiB and Tib which go up in ratios of 2¹⁰ = 1024. Knowing that removes the need to use Math#pow, a Amit has already told you. It probably doesn't matter whether you use 1024 or 1000, but maybe you should ask about that.

Greenhorn
Posts: 1
• Number of slices to send:
Optional 'thank-you' note:
Hi Joseph Abney,

Replace //Calculations with the following code:

*** Also try to take all variableas as Long type because int has limited range. Hope you will get the desired output

Campbell Ritchie
Marshal
Posts: 72993
330
• Number of slices to send:
Optional 'thank-you' note:
Welcome to the Ranch

Please do not try to give complete solutions. Please read what it says on the title page of this forum:

We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.

It does not help to give a complete solution before the original poster has had a chance to work out their own solution.
Don't be annoyed but I have pulled rank and deleted the answer.

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?
reply