# Java program to convert celsius to fahrenheit and vice versa using methods – Java Program to Convert Celsius to Fahrenheit and Fahrenheit to Celsius

java program to convert celsius to fahrenheit and vice versa using methods: In the previous article we have discussed about Java Program to Convert Celsius to kelvin and Kelvin to Celsius

In this article we will see how to convert Celsius to Fahrenheit and Fahrenheit to Celsius by using Java programming language.

## Java Program to Convert Celsius to Fahrenheit and Fahrenheit to Celsius

Convert celsius to fahrenheit javaBefore jumping into the program let’s know the relationship between Celsius and Fahrenheit and how we can convert Celsius to Fahrenheit and vice versa.

Generally Celsius and Fahrenheit are used as unit in case of temperature measurement.

1 Celsius =  33.8 Fahrenheit
1 Fahrenheit =  -17.2222 Celsius

Formula to convert Celsius to Fahrenheit.

Fahrenheit = Celsius*1.8+32

Formula to convert Fahrenheit to Celsius.

Celsius = 5/9 * (Fahrenheit-32)

Let’s see different ways to convert Celsius to Fahrenheit and Fahrenheit to Celsius.

### Method-1: Java Program to Convert Celsius to Fahrenheit and Fahrenheit to Celsius By Using Static Input Value

Approach:

• Declare Celsius and Fahrenheit value.
• Then convert Celsius to Fahrenheit and Fahrenheit to Celsius by using the formula.
• Print result.

Program:

public class Main
{
public static void main(String args[])
{
//value of fahrenheit declared
double fahrenheit = 1;
//value of celsius declared
double celsius = 1;

//converting celsius to fahrenheit
double f = celsius*1.8+32;
//converting fahrenheit to celsius
double c = (fahrenheit-32)/1.8;
//printing result
System.out.println("Value of "+fahrenheit+" fahrenheit in celsius: "+ c);
System.out.println("Value of "+celsius+" celsius in fahrenheit: "+ f);
}
}
Output:

Value of 1.0 fahrenheit in celsius: -17.22222222222222
Value of 1.0 celsius in fahrenheit: 33.8

### Method-2: Java Program to Convert Celsius to Fahrenheit and Fahrenheit to Celsius By Using User Input Value

Approach:

• Take user input of Celsius and Fahrenheit value.
• Then convert Celsius to Fahrenheit and Fahrenheit to Celsius by using the formula.
• Print result.

Program:

import java.util.*;
public class Main
{
public static void main(String args[])
{
//Scanner class object created
Scanner sc=new Scanner(System.in);
//Taking the value input of double variable celsius
System.out.println("Enter value of celsius: ");
double celsius = sc.nextDouble();
//Taking the value input of double variable fahrenheit
System.out.println("Enter value of fahrenheit: ");
double fahrenheit = sc.nextDouble();

//converting celsius to fahrenheit
double f = celsius*1.8+32;
//converting fahrenheit to celsius
double c = (fahrenheit-32)/1.8;
//printing result
System.out.println("Value of "+fahrenheit+" fahrenheit in celsius: "+ c);
System.out.println("Value of "+celsius+" celsius in fahrenheit: "+ f);
}
}
Output:

Enter value of celsius:
5
Enter value of fahrenheit:
5
Value of 5.0 fahrenheit in celsius: -15.0
Value of 5.0 celsius in fahrenheit: 41.0


### Method-3: Java Program to Convert Celsius to Fahrenheit and Fahrenheit to Celsius By Using User Defined Method

Approach:

• Take user input of Celsius and Fahrenheit value.
• Call a user defined method by passing Celsius and Fahrenheit value as parameter.
• Inside method convert Celsius to Fahrenheit and vice versa by using the formula.
• Print result.

Program:

import java.util.*;
public class Main
{
public static void main(String args[])
{
//Scanner class object created
Scanner sc=new Scanner(System.in);
//Taking the value input of double variable celsius
System.out.println("Enter value of celsius: ");
double celsius = sc.nextDouble();
//Taking the value input of double variable fahrenheit
System.out.println("Enter value of fahrenheit: ");
double fahrenheit = sc.nextDouble();
//calling user defined method convert()
convert(celsius, fahrenheit);
}

//convert() method to convert Celsius to fahrenheit and vice versa
public static void convert(double celsius, double fahrenheit)
{

//converting celsius to fahrenheit
double f = celsius*1.8+32;
//converting fahrenheit to celsius
double c = (fahrenheit-32)/1.8;
//printing result
System.out.println("Value of "+fahrenheit+" fahrenheit in celsius: "+ c);
System.out.println("Value of "+celsius+" celsius in fahrenheit: "+ f);
}
}
Output:

Enter value of celsius:
15
Enter value of fahrenheit:
4
Value of 4.0 fahrenheit in celsius: -15.555555555555555
Value of 15.0 celsius in fahrenheit: 59.0

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

Related Java Programs: