Convert Celsius into Fahrenheit in Java Program | Function
ADVERTISEMENTS
Convert celsius into fahrenheit in Java program.
In this article, you will learn how to convert celsius into fahrenheit in Java program.
Example
Enter the temperature in Celsius::
7
7.0 Celsius = 44.6 Fahrenheit
Standard Formula
F = (C * 9/5) + 32
You should have knowledge of the following topics in Java programming to understand this program:
- Java
java.util.Scanner
package - Java
main()
method - Java
System.out.println()
function
Source Code
// Convert Celsius into Fahrenheit in Java Program
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the temperature in Celsius::\n");
float c = in.nextFloat();
float f;
// c = celsius, f = fahrenheit
// It will convert the celsius into fahrenheit
f = (float)((c * 9 / 5) + 32);
// It will print the final output
System.out.println(c + " Celsius = " + f + " Fahrenheit");
}
}
Output
Enter the temperature in Celsius::
7
7.0 Celsius = 44.6 Fahrenheit
Explanation
In this given program, we have taken input 36
from the user via the system console. Then we applied this standard formula F = (C * 9 / 5) + 32
to make calculations.
Then this program will print the final output 44.6 Fahrenheit
.
Convert Celsius into Fahrenheit in Java Program using Function
// Convert Celsius into Fahrenheit in Java Program using Function
import java.util.Scanner;
public class Main {
// This function will convert the celsius into fahrenheit
public static float CelcToFahren(float x) {
return (float)((x * 9 / 5) + 32);
}
// It's the driver function
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the temperature in Celsius::\n");
float c = in.nextFloat(); // c = celsius
// It will print the final output
System.out.println(c + " Celsius = " + CelcToFahren(c) + " Fahrenheit");
}
}
Output
Enter the temperature in Celsius::
10
10.0 Celsius = 50.0 Fahrenheit