Reputation: 53
public class p6
{
public static void main(String[] args)
{
System.out.println("celsius \t Fahrenheit \t | Fahrenheit \t\t Celsius");
for (double i = 31; i <= 40; i++) {
System.out.printf(i + "\t\t" + "\t" + "%.2f", +celsiusToFahrenheit(i));
System.out.println("");
}
for (j = 30; j <= 120; j++) {
System.out.printf("\t\t\t\t" +j + "\t\t\t\t" + "%.2f",+fahrenheitToCelsius(j));
System.out.println("");
}
}
}
// • Converts a Celsius value to Fahrenheit
// • Converts a Celsius value to Fahrenheit
public static double celsiusToFahrenheit(double celsius)
{
return (9.0 / 5) * celsius + 32;
}
// Converts a Fahrenheit value to Celsius
public static double fahrenheitToCelsius(double fahrenheit)
{
return (5.0 / 9) * (fahrenheit - 32);
}
}
This is the output, I am getting but I need it to be all in order.
celsius Fahrenheit | Fahrenheit Celsius
31.0 87.80
32.0 89.60
33.0 91.40
34.0 93.20
35.0 95.00
36.0 96.80
37.0 98.60
38.0 100.40
39.0 102.20
40.0 104.00
30.0 -1.11
31.0 -0.56
32.0 0.00
33.0 0.56
34.0 1.11
35.0 1.67
36.0 2.22
37.0 2.78
38.0 3.33
39.0 3.89
40.0 4.44
41.0 5.00
42.0 5.56
43.0 6.11
44.0 6.67
45.0 7.22
46.0 7.78
47.0 8.33
48.0 8.89
49.0 9.44
50.0 10.00
51.0 10.56
52.0 11.11
53.0 11.67
54.0 12.22
55.0 12.78
56.0 13.33
57.0 13.89
58.0 14.44
59.0 15.00
60.0 15.56
61.0 16.11
62.0 16.67
63.0 17.22
64.0 17.78
65.0 18.33
66.0 18.89
67.0 19.44
68.0 20.00
69.0 20.56
70.0 21.11
71.0 21.67
72.0 22.22
73.0 22.78
74.0 23.33
75.0 23.89
76.0 24.44
77.0 25.00
78.0 25.56
79.0 26.11
80.0 26.67
81.0 27.22
82.0 27.78
83.0 28.33
84.0 28.89
85.0 29.44
86.0 30.00
87.0 30.56
88.0 31.11
89.0 31.67
90.0 32.22
91.0 32.78
92.0 33.33
93.0 33.89
94.0 34.44
95.0 35.00
96.0 35.56
97.0 36.11
98.0 36.67
99.0 37.22
100.0 37.78
101.0 38.33
102.0 38.89
103.0 39.44
104.0 40.00
105.0 40.56
106.0 41.11
107.0 41.67
108.0 42.22
109.0 42.78
110.0 43.33
111.0 43.89
112.0 44.44
113.0 45.00
114.0 45.56
115.0 46.11
116.0 46.67
117.0 47.22
118.0 47.78
119.0 48.33
120.0 48.89
I need to get the output in a table, but the problem i am having right now is that the the numbers are being repeated so many times each. The loop does stop, what it is not I need to get. If i used a different loop it works fine but I need to the the output in two different columns.
I am converting celsius to fahrenheit, so in one column should display celsius and another the fahrenheit.
Then I am convering F to C, so in another column I need to display F and another the C.
I need to get one column with celsius and another column with
The four columns have to be next to each other.
Upvotes: 0
Views: 76
Reputation: 393821
First of all, you need a single loop, not a nested loop.
Second of all, in order for each F to C conversion to have a corresponding C to F conversion in the same row, i
and j
must have the same number of values.
Here's a possible way to do it :
double j = 30;
for (double i = 31; i <= 40; i++) {
System.out.print(i + "\t\t" + "\t" +celsiusToFahrenheit(i)+"\t\t\t" +j + "\t\t\t\t"+fahrenheitToCelsius(j));
System.out.println("");
j+=10; // since i has 10 values (31 to 40), j must increment by 10 in each
// iteration in order for it to have 10 values between 30 and 120
}
The output:
celsius Fahrenheit | Fahrenheit Celsius
31.0 87.80000000000001 30.0 -1.1111111111111112
32.0 89.6 40.0 4.444444444444445
33.0 91.4 50.0 10.0
34.0 93.2 60.0 15.555555555555557
35.0 95.0 70.0 21.11111111111111
36.0 96.8 80.0 26.666666666666668
37.0 98.60000000000001 90.0 32.22222222222222
38.0 100.4 100.0 37.77777777777778
39.0 102.2 110.0 43.333333333333336
40.0 104.0 120.0 48.88888888888889
It still requires a minor fix in the formatting, but it's very close to what you wanted.
Better formatting:
System.out.println("celsius \tFahrenheit\t | \tFahrenheit \t\tCelsius");
double j = 30;
for (double i = 31; i <= 40; i++) {
System.out.printf("%.2f\t\t%.2f\t\t\t%.2f\t\t\t%.2f", i, celsiusToFahrenheit(i),j,fahrenheitToCelsius(j));
System.out.println("");
j+=10; // since i has 10 values (31 to 40), j must increment by 10 in each
// iteration in order for it to have 10 values between 30 and 120
}
Output:
celsius Fahrenheit | Fahrenheit Celsius
31.00 87.80 30.00 -1.11
32.00 89.60 40.00 4.44
33.00 91.40 50.00 10.00
34.00 93.20 60.00 15.56
35.00 95.00 70.00 21.11
36.00 96.80 80.00 26.67
37.00 98.60 90.00 32.22
38.00 100.40 100.00 37.78
39.00 102.20 110.00 43.33
40.00 104.00 120.00 48.89
Upvotes: 3