Program to convert KiloBytes to Bytes and Bits

Given Number of Kilobytes. The task is to Convert them into Bytes and Bits.

Bit: A Bit is the basic unit in computer information and has only two different values, normally defined as a 0 or 1. These values can be interpreted as on or off, yes or no, true or false, etc. It just depends on the binary code.
Add 1 bit, double the number of patterns.

1 bit – 2 patterns i.e 0 and 1
2 bits – 4 patterns i.e 00, 01, 10, 11
3 bits – 8 patterns i.e 000, 001, 010, 011, 100, 101, 110, 111



Mathematically: n bits yields 2n patterns.

Bytes: A Byte is just 8 Bits and is the smallest unit of memory that can be addressed in many computer systems.
Important points about Bytes:

  • One byte can store one character, e.g. ‘A’ or ‘x’ or ‘$’ etc.
  • 1 byte i.e 8 bits can make 256 different patterns.
  • One byte can hold a number between 0 and 255.
  • Different Form:-
    1. Kilobyte, KB, about 1 thousand bytes.
    2. Megabyte, MB, about 1 million bytes.
    3. Gigabyte, GB, about 1 billion bytes.
    4. Terabyte, TB, about 1 trillion bytes
  • Examples:

    Input: kilobytes = 1
    Output: 1 Kilobytes = 1024 Bytes and 8192 Bits.
    
    Input: kilobytes = 8
    Output: 8 Kilobytes = 8192 Bytes and 65536 Bits.
    

    Below is the program to convert KilloBytes to Bytes and Bits:

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ implementation of above program
    #include <bits/stdc++.h>
    using namespace std;
      
    // Function to calculates the bits
    long Bits(int kilobytes)
    {
        long Bits = 0;
      
        // calculates Bits
        // 1 kilobytes(s) = 8192 bits
        Bits = kilobytes * 8192;
      
        return Bits;
    }
      
    // Function to calculates the bytes
    long Bytes(int kilobytes)
    {
        long Bytes = 0;
      
        // calculates Bytes
        // 1 KB = 1024 bytes
        Bytes = kilobytes * 1024;
      
        return Bytes;
    }
      
    // Driver code
    int main()
    {
        int kilobytes = 1;
      
        cout << kilobytes << " Kilobytes = "
             << Bytes(kilobytes) << " Bytes and "
             << Bits(kilobytes) << " Bits.";
        return 0;
    }

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java implementation of above program
      
    import java.util.*;
    import java.lang.*;
    import java.io.*;
    import java.math.BigInteger;
      
      
    class GFG
    {
       
    // Function to calculates the bits
    static BigInteger Bits(int kilobytes)
    {
        BigInteger  Bits = new BigInteger("0");
       
        // calculates Bits
        // 1 kilobytes(s) = 8192 bits
          
        BigInteger kilo = BigInteger.valueOf(kilobytes);
        Bits = kilo.multiply(BigInteger.valueOf(8192));
       
        return Bits;
    }
       
    // Function to calculates the bytes
    static BigInteger Bytes(int kilobytes)
    {
        BigInteger Bytes = new BigInteger("0");
       
        // calculates Bytes
        // 1 KB = 1024 bytes
          
       BigInteger kilo = BigInteger.valueOf(kilobytes);
       Bytes = kilo.multiply(BigInteger.valueOf(1024));
       
        return Bytes;
    }
       
    // Driver code
    public static void main(String args[])
    {
        int kilobytes = 1;
       
        System.out.print(kilobytes + " Kilobytes = "
             + Bytes(kilobytes) + " Bytes and "
             + Bits(kilobytes) + " Bits.");
    }
    }

    chevron_right

    
    

    Python3

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Python implementation of above program
      
    # Function to calculates the bits 
    def Bits(kilobytes) :
      
        # calculates Bits 
        # 1 kilobytes(s) = 8192 bits 
        Bits = kilobytes * 8192
      
        return Bits
      
    # Function to calculates the bytes 
    def Bytes(kilobytes) :
      
        # calculates Bytes 
        # 1 KB = 1024 bytes 
        Bytes = kilobytes * 1024
      
        return Bytes
      
    # Driver code
    if __name__ == "__main__" :
      
        kilobytes = 1
      
        print(kilobytes, "Kilobytes ="
        Bytes(kilobytes) , "Bytes and"
        Bits(kilobytes), "Bits")
      
    # This code is contributed by ANKITRAI1

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# implementation of above program 
    using System;
      
    class GFG
    {
          
    // Function to calculates the bits 
    static long Bits(int kilobytes) 
        long Bits = 0; 
      
        // calculates Bits 
        // 1 kilobytes(s) = 8192 bits 
        Bits = kilobytes * 8192; 
      
        return Bits; 
      
    // Function to calculates the bytes 
    static long Bytes(int kilobytes) 
        long Bytes = 0; 
      
        // calculates Bytes 
        // 1 KB = 1024 bytes 
        Bytes = kilobytes * 1024; 
      
        return Bytes; 
      
    // Driver code 
    static public void Main ()
    {
        int kilobytes = 1; 
      
        Console.WriteLine (kilobytes +" Kilobytes = "+
                     Bytes(kilobytes) + " Bytes and "+
                      Bits(kilobytes) + " Bits."); 
    }
    }
      
    // This code is contributed by Sach_Code

    chevron_right

    
    

    PHP

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    // PHP implementation of above program 
      
    // Function to calculates the bits 
    function Bits($kilobytes
        $Bits = 0; 
      
        // calculates Bits 
        // 1 kilobytes(s) = 8192 bits 
        $Bits = $kilobytes * 8192; 
      
        return $Bits
      
    // Function to calculates the bytes 
    function Bytes($kilobytes
        $Bytes = 0; 
      
        // calculates Bytes 
        // 1 KB = 1024 bytes 
        $Bytes = $kilobytes * 1024; 
      
        return $Bytes
      
    // Driver code 
    $kilobytes = 1; 
      
    echo $kilobytes;
    echo (" Kilobytes = ");
    echo Bytes($kilobytes);
    echo (" Bytes and ");
    echo Bits($kilobytes);
    echo (" Bits.");
      
    // This code is contributed
    // by Shivi_Aggarwal 
    ?>

    chevron_right

    
    

    Output:

    1 Kilobytes = 1024 Bytes and 8192 Bits.
    


    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.