Java Program to Convert Byte to Kilobyte

In the previous article, we have discussed about Java Program to Convert Kilobyte to Bytes and Bits

In this article we will see how to convert bytes to kilobytes by using Java programming language.

Java Program to Convert Byte to Kilobyte

Before jumping into the program let’s know the relationship between kilobytes and bytes and how we can convert bytes to kilobytes.

1 Kilobyte = 1024 Bytes

Formula to convert bytes to kilobytes.

Kilobytes = Bytes / 1024

(OR)

Kilobytes = Bytes * 0.000976563

Let’s see different ways to find convert bytes to kilobytes.

Method-1: Java Program to Convert Byte to Kilobyte By Using Static Input Value

Approach:

  • Declare user input bytes value.
  • Then convert bytes to kilobytes by using the formula.
  • Print result.

Program:

public class Main 
{
   public static void main(String args[])
   {
        //byteValue declared
         double byteValue = 1024;
        
        //calculating kilobytes value
         double kilobytes  =  byteValue * 0.000976563; 
        
        //printing result
        System.out.println("Value of " +byteValue+ " byte in kilobyte: "+ kilobytes);   
   }
}
Output:

Value of 1024.0 byte in kilobyte: 1.000000512

Method-2: Java Program to Convert Byte to Kilobyte By Using User Input Value

Approach:

  • Take user input of kilobyte value.
  • Then convert bytes to kilobytes by using the formula.
  • Print result.

Program:

import java.util.*;

public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //Taking the value input of double variable byteValue
        System.out.println("Enter value of bytes: ");  
        double byteValue = sc.nextDouble();
        
        //calculating kilobytes value
        double kilobytes  =  byteValue / 1024; 
        
        //printing result
        System.out.println("Value of " +byteValue+ " byte in kilobyte: "+ kilobytes);   
   }
}
Output:

Enter value of bytes: 
1024
Value of 1024.0 byte in kilobyte: 1.0

Method-3: Java Program to Convert Byte to Kilobyte By Using User Defined Method

Approach:

  • Take user input of bytes value.
  • Call a user defined method by passing bytes value as parameter.
  • Inside method convert bytes to kilobytes by using the formula.
  • Print result.

Program:

import java.util.*;

public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //Taking the value input of double variable byteValue
        System.out.println("Enter value of bytes: ");  
        double byteValue = sc.nextDouble();
        
         //calling user defined method convert()
        convert(byteValue);
   }
   
    //convert() method to convert bytes to kilobytes
    public static void convert(double byteValue)
    {
        //calculating kilobytes value
        double kilobytes  =  byteValue / 1024; 
        //printing result
        System.out.println("Value of " +byteValue+ " byte in kilobyte: "+ kilobytes);   
   }
}
Output:

Enter value of bytes: 
20000
Value of 2000.0 byte in kilobyte: 1.953125

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Related Java Programs: