Example Lab 2.2
[1] Question :
(Converting Celsius to Fahrenheit)
Write a program that reads a Celsius degree in double from the console, then converts it to Fahrenheit and displays the result.
The formula for the conversion is as follows:
fahrenheit = (9 / 5) * celsius + 32
[2] Answer :
-------------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
public class Lab2_2 {
public static void main(String[] args) {
// Step 1 : Prompt the input
Scanner input = new Scanner(System.in);
// Step 2 : Enter a degree in Celsius
System.out.print("Enter a degree in Celsius: ");
double celsius = input.nextDouble();
// Step 3 : Calculation
double fahranheit = ((9.0/5)* celsius) + 32;
// Step 4 : Display Celsius in Fahrenheit
System.out.println( (int) celsius + " Celsius is " + fahranheit + " Fahranheit" );
}
}
--------------------------------------------------------------------------------------------------------------------------
[3] Output :
Enter a degree in Celsius: 43
43 Celsius is 109.4 Fahrenheit
DCT 1093 - Lab 2 - Elementary Programming
Thursday, 10 January 2013
You can leave a response
zT-FKMPT
Jabatan Sains Komputer . Fakulti Komputer, Media, & Pengurusan Teknologi . TATIUC