Java Program for Conversion Between Kilograms and Grams

Weight Kilogram Gram

Java programs for conversion between kilograms and grams have been given below. These are the units to calculate mass of an object. To convert kilograms into grams, multiply the number of kilograms by 1000 and to convert grams into kilograms, divide the number of grams by 1000.


Example:

Convert kilograms to grams:

(i) 1 kilogram = (1 x 1000) grams = 1000 grams

(ii) 2.5 kilograms = (2.5 x 1000) grams = 2500 grams

(iii) 1.875 kilograms = (1.875 x 1000) grams = 1875 grams


Convert gram to kilogram:

(i)1 gram = (1 / 1000) kilograms = 0.001 kilograms

(ii)300 grams = (300 / 1000) kilograms = 0.300 kilograms

(iii)1250 grams = (1250 / 1000) kilograms = 1.250 kilograms



The following section covers the Java programs to solve the above problems are along with the algorithm, pseudocode and time-complexity of the programs.





1. Algorithms for Conversion between Kilograms and Grams




1.1. Algorithm to convert Kilograms to Grams


1. Take value in kilograms k as input.

2. Perform g = k * 1000 to convert kilograms to grams

3. Display value of g as the output.




1.2. Algorithm to convert grams to Kilograms


1. Take value in grams g as input.

2. Perform k = g / 1000 to convert grams to kilograms

3. Display value of k as the output.




2. Pseudocode for Conversion between Kilograms and Grams




2.1. Pseudocode to convert Kilograms to Grams


Input : Value in Kilograms $k$

Output : $k$ in grams

1. Procedure kilogramToGram($k$):

2. $g \leftarrow k \times 1000$

3. Return $g$

4. End Procedure





2.2. Pseudocode to convert grams to Kilograms


Input : Value in Grams $g$

Output : $g$ in Kilograms

1. Procedure gramToKilogram($g$):

2. $k \leftarrow \frac{g}{1000}$

3. Return $k$

4. End Procedure





3. Time complexity for Conversion between Kilograms and Grams


Time Complexity: O(1)




4. Programs for Conversion between Kilograms and Grams




4.1. Java program to convert Kilograms to Grams

Code has been copied
/******************************************
	    alphabetacoder.com
Java program to convert Kilograms to Grams 
*******************************************/

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare variables
        float g, k;

        // take inputs
        System.out.print("Enter values in kilograms: ");
        k = sc.nextFloat();

        // convert kilograms to grams
        g = k * 1000;

        // display result
        System.out.println(k + " kilograms = " + g + " grams");
    }
}

Output


Enter values in kilograms: 12.255

12.255 kilograms = 12255.0 grams





4.2. Java program to convert Grams to Kilograms

Code has been copied
/******************************************
	     alphabetacoder.com
Java program to convert Grams to Kilograms 
*******************************************/

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare variables
        float g, k;

        // take inputs
        System.out.print("Enter values in grams: ");
        g = sc.nextFloat();

        // convert grams to kilograms
        k = g / 1000;

        // display result
        System.out.println(g + " grams = " + k + " kilograms");
    }
}

Output


Enter values in grams: 3525

3525.0 grams = 3.525 kilograms