Convert this code Java code to C++ and get the same output: public class Problem3 { // method to accept a temperature in fahrenheit and convert it to celsius public static double fahrenheitToCelsius(double fahrenheit) { // declaring a variable to store the temperature in celsius double celsius; // converting temperature to celsius using formula C/5 = (F-32)/9 celsius = 5 * (fahrenheit - 32) / 9; // returning temperature in celsius return celsius; } // method to accept a temperature in celsius and convert it to fahrenheit public static double celsiusToFahrenheit(double celsius) { // declaring a variable to store the temperature in fahrenheit double fahrenheit; // converting temperature to fahrenheit using formula C/5 = (F-32)/9 fahrenheit = 9 * celsius / 5 + 32; // returning temperature in fahrenheit return fahrenheit; } public static void main(String[] args) { // variables to store temperature, temperatur in fahrenheit and in celsius double temperature, fahrenheit, celsius; // displaying header of the conversion table System.out.println("\n Temperature Convertion Tables\n"); System.out.println(" Temperature | Temperature"); System.out.println(" (degrees) | (degrees)"); System.out.println(" F C | C F"); // initializing variable temperature with -40 temperature = -40; // loop will continue for 100 times for (int i = 1; i <= 100; i++) { // converting same tempertature to fahrenheit and celsius fahrenheit = celsiusToFahrenheit(temperature); celsius = fahrenheitToCelsius(temperature); // displaying formatted output System.out.format("%10.3f %10.3f | %10.3f %10.3f\n", temperature, celsius, temperature, fahrenheit); // incrementing temperature by 5 temperature += 5; } }}
Convert this code Java code to C++ and get the same output:
public class Problem3 {
// method to accept a temperature in fahrenheit and convert it to celsius
public static double fahrenheitToCelsius(double fahrenheit) {
// declaring a variable to store the temperature in celsius
double celsius;
// converting temperature to celsius using formula C/5 = (F-32)/9
celsius = 5 * (fahrenheit - 32) / 9;
// returning temperature in celsius
return celsius;
}
// method to accept a temperature in celsius and convert it to fahrenheit
public static double celsiusToFahrenheit(double celsius) {
// declaring a variable to store the temperature in fahrenheit
double fahrenheit;
// converting temperature to fahrenheit using formula C/5 = (F-32)/9
fahrenheit = 9 * celsius / 5 + 32;
// returning temperature in fahrenheit
return fahrenheit;
}
public static void main(String[] args) {
// variables to store temperature, temperatur in fahrenheit and in celsius
double temperature, fahrenheit, celsius;
// displaying header of the conversion table
System.out.println("\n Temperature Convertion Tables\n");
System.out.println(" Temperature | Temperature");
System.out.println(" (degrees) | (degrees)");
System.out.println(" F C | C F");
// initializing variable temperature with -40
temperature = -40;
// loop will continue for 100 times
for (int i = 1; i <= 100; i++) {
// converting same tempertature to fahrenheit and celsius
fahrenheit = celsiusToFahrenheit(temperature);
celsius = fahrenheitToCelsius(temperature);
// displaying formatted output
System.out.format("%10.3f %10.3f | %10.3f %10.3f\n", temperature, celsius, temperature, fahrenheit);
// incrementing temperature by 5
temperature += 5;
}
}
}
Step by step
Solved in 2 steps