➤ Check Even Number
➤ Check Odd Number
➤ Java Even-Odd
➤ Greatest of 3 numbers
➤ Exponents in Java
➤ Java Leap Year Program
➤ Display Multiplication Table
➤ Reverse of a number
➤ Factors of a Number
➤ Java LCM of 2 Numbers
➤ Java HCF of 2 Numbers
➤ Quadratic Equation Program
➤ Square Root of Number
➤ Perfect Square Program
➤ Simple Calculator Program
➤ BMI Calculator Java
➤ Factorial of a Number
➤ Factorial Using Recursion
# Java Programs to Find Sum
# Java Conversion Programs
# Java Program on Series
# Java Pattern Programs
# Java Number Programs
Java Array Programs
Java String Programs
Conversion from Celsius to Fahrenheit and from Fahrenheit to Celsius has an important role in the conversion of units system. In this post, we will develop a Java program to convert Celsius to Fahrenheit.
The formula used to convert from Celsius to Fahrenheit is given as, ⁰F= (⁰C * 9/5) + 32
Java Program to Convert Celsius to Fahrenheit
import java.util.Scanner;
public class TemperatureConversion {
// method to convert celsius to fahrenheit
private static double tofahrenheit(double celsius){
return ( celsius * (9.0/5.0) + 32 );
}
public static void main(String[] args) {
// declare variables
double celsius = 0.0;
double fahrenheit = 0.0;
// create Scanner class object to read input
Scanner scan = new Scanner(System.in);
// read input
System.out.print("Enter Celsius value:: ");
celsius = scan.nextDouble();
// convert temperature
fahrenheit = tofahrenheit(celsius);
// display result
System.out.println("Fahrenheit value = "
+fahrenheit);
// close Scanner class object
scan.close();
}
}
The output for different test-cases:-
Enter Celsius value:: 15
Fahrenheit value = 59.0
Enter Celsius value:: -40
Fahrenheit value = -40.0
Note:- The -40⁰C is equal to the -40⁰F.
Also See:-
- Simple Calculator Program
- BMI (Body Mass Index) Calculator
- Electric Bill Java Program
- Factorial of a Number in Java
- Factorial Using Recursion in Java
- Fibonacci Series program in Java
Java Program to convert Celsius to Fahrenheit In a range
Generally, we need to convert Celsius to Fahrenheit in between a range. So, now we will Convert Celsius to Fahrenheit in between two numbers like for Celsius = 0,20,40,60,….,200. There are various ways to do this like using while loop or using for loop, or do-while loop.
import java.util.Scanner;
public class TemperatureConversionInRange {
// method to convert celsius to fahrenheit
public static double tofahrenheit(double celsius){
return ( celsius * (9.0/5.0) + 32 );
}
public static void main(String[] args) {
// declare variables
int lower, upper, step;
// create Scanner class object
Scanner scan = new Scanner(System.in);
// read input
System.out.print("Enter Lower limit:: ");
lower = scan.nextInt();
System.out.print("Enter Upper limit:: ");
upper = scan.nextInt();
System.out.print("Enter step:: ");
step = scan.nextInt();
// convert temperature
System.out.println("Celsius \t"+
"Fahrenheit");
for(int i=lower; i<=upper; i+=step)
// display result
System.out.println( i + "\t\t"
+ tofahrenheit(i) );
// close Scanner class object
scan.close();
}
}
The output for different test-cases:-
Enter Lower limit:: 0Enter
Upper limit:: 200
Enter step:: 20
Celsius Fahrenheit
0 32.0
20 68.0
40 104.0
60 140.0
80 176.0
100 212.0
120 248.0
140 284.0
160 320.0
180 356.0
200 392.0
Enter Lower limit:: -50Enter
Upper limit:: 50
Enter step:: 10
Celsius Fahrenheit
-50 -58.0
-40 -40.0
-30 -22.0
-20 -4.0
-10 14.0
0 32.0
10 50.0
20 68.0
30 86.0
40 104.0
50 122.0
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!