-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e51f37
commit 6dd0d2f
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
Introduction to Java/Lecture 8/Functions and Scope/Exercises/FahrenheitToCelciusTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Fahrenheit to Celsius Table | ||
Given three values - Start Fahrenheit Value (S), End Fahrenheit value (E) and Step Size (W), you need to convert all Fahrenheit values from Start to End at the gap of W, into their corresponding Celsius values and print the table. | ||
Input Format : | ||
3 integers - S, E and W respectively | ||
Output Format : | ||
Fahrenheit to Celsius conversion table. One line for every Fahrenheit and Celsius Fahrenheit value. Fahrenheit value and its corresponding Celsius value should be separate by tab ("\t") | ||
Constraints : | ||
0 <= S <= 1000 | ||
0 <= E <= 1000 | ||
0 <= W <= 1000 | ||
Sample Input 1: | ||
0 | ||
100 | ||
20 | ||
Sample Output 1: | ||
0 -17 | ||
20 -6 | ||
40 4 | ||
60 15 | ||
80 26 | ||
100 37 | ||
Sample Input 2: | ||
120 | ||
200 | ||
40 | ||
Sample Output 2: | ||
120 48 | ||
160 71 | ||
200 93 | ||
Explanation for Sample Output 2 : | ||
Start value is 120, end value is 200 and step size is 40. Therefore, the values we need to convert are 120, 120 + 40 = 160, and 160 + 40 = 200. | ||
The formula for converting Fahrenheit to Celsius is: | ||
Celsius Value = (5/9)*(Fahrenheit Value - 32) | ||
Plugging 120 into the formula, the celsius value will be (5 / 9)*(120 - 32) => (5 / 9) * 88 => (5 * 88) / 9 => 440 / 9 => 48.88 | ||
But we'll only print 48 because we are only interested in the integral part of the value. | ||
--------------------------------------------------------------------------------------------------------- | ||
*/ | ||
|
||
|
||
import java.util.Scanner; | ||
public class FahrenheitToCelciusTable { | ||
|
||
public class Solution { | ||
public static void printFahrenheitTable(int start, int end, int step) { | ||
for(int fahrenheight = start;fahrenheight <= end;fahrenheight += step){ | ||
|
||
int celcius = (int)((5.0/9)*(fahrenheight-32)); | ||
System.out.println(fahrenheight + "\t"+ celcius); | ||
|
||
} | ||
|
||
} | ||
} | ||
|
||
//pregiven code | ||
public static void main(String[] args) | ||
{ | ||
// TODO Auto-generated method stub | ||
Scanner s = new Scanner(System.in); | ||
int start = s.nextInt(); | ||
int end = s.nextInt(); | ||
int step = s.nextInt(); | ||
Solution.printFahrenheitTable(start, end, step); | ||
s.close(); | ||
|
||
} | ||
|
||
} |