Java Arithmetic Operator convert pounds into kilograms

Question

We would like to write a program that converts pounds into kilograms.

The program prompts the user to enter a number in pounds, converts it to kilograms.

Display the result.

One pound is 0.454 kilograms.

import java.util.Scanner;

public class Main {

  public static void main(String[] Strings) {

    Scanner input = new Scanner(System.in);
    System.out.print("Enter a number in pounds: ");

    //your code/*from   ww  w.  ja va  2s. c  o  m*/
  }
}


import java.util.Scanner;

public class Main {

  public static void main(String[] Strings) {

    Scanner input = new Scanner(System.in);
    System.out.print("Enter a number in pounds: ");

    double pounds = input.nextDouble();
    double kilograms = pounds * 0.454;

    System.out.println(pounds + " pounds is " + kilograms + " kilograms.");
  }
}

Note

Define a method to do the conversion.

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a number in pounds: ");
    double pounds = input.nextDouble();

    double kilograms = poundsToKilograms(pounds);

    System.out.println(pounds + " pounds is " + kilograms + " kilograms");
  }//w  w  w . j a v a  2 s.  c  om

  private static double poundsToKilograms(double pounds) {
    return pounds * 0.454;
  }
}

Create a constant for the conversion.


import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);   // Create new Scanner object
    final double KILOGRAMS_PER_POUND = 0.454; // Create constant value

    // Prompt user to enter the number of pounds
    System.out.print("Enter a number in pounds: ");
    double pounds = input.nextDouble();

    // Convert pounds into kilograms
    double kilograms = pounds * KILOGRAMS_PER_POUND;

    // Display the results
    System.out.println(pounds + " pounds is " + kilograms + " kilograms");
  }/*www  .java2s  .c  o  m*/
}



PreviousNext

Related