Program to Convert Fahrenheit into Celsius [C, C++, Python & Java]

Posted in

Program to Convert Fahrenheit into Celsius [C, C++, Python & Java]
vinaykhatri

Vinay Khatri
Last updated on November 21, 2024

    In this tutorial, we will learn to write a program in C, C++, Python, and Java to convert Fahrenheit into Celsius and vice-versa. We will ask the user whether to convert Fahrenheit into Celsius or Celsius into Fahrenheit. Based on their chosen option, we will write the code that will perform the conversion.

    To write the program, you need knowledge of the if statement, the programming language syntax, and the conversion formulas (Fahrenheit into Celsius and Celsius into Fahrenheit).

    Conversion Formula

    Celsius to Fahrenheit:   (°C × 9/ 5 ) + 32 = °F

    Fahrenheit to Celsius:   (°F ? 32) × 5/ 9 = °C

    C Program to Convert Fahrenheit into Celsius

    #include<stdio.h> 
    
    int main() 
    {
       float f, c;
       int option;
       printf("Enter 1 to change temperature from Fahrenheit to Celsius\n");
       printf("Enter 2 to change temperature from Celsius to Fahrenheit\n");
       scanf("%d", &option);
       
       if(option ==1)
       {
       	printf("Enter Temperature in Fahrenheit: ");
       	scanf("%f", &f);
       	c= (f-32)/1.8;
       	printf("%.2f C",c);
       }
       if(option==2)
       {
       	printf("Enter Temperature in Celsius: ");
       	scanf("%f", &c);
       	f=(1.8*c)+32;
       	printf("%.2f F",f);
       }
    
        return 0;
    }

    Output

    Enter 1 to change temperature from Fahrenheit to Celsius
    Enter 2 to change temperature from Celsius to Fahrenheit
    1
    Enter Temperature in Fahrenheit: 34
    1.11 C

    C++ Program to Convert Fahrenheit into Celsius

    #include<iostream>
    using namespace std;
    int main()
    {
        float ut,ct;
        int ch;
        cout<<"Enter 1 to change temperature from Fahrenheit to Celsius\n";
        cout<<"Enter 2 to change temperature from Celsius to Fahrenheit\n";
        cin>>ch;
        if (ch==1)
        {
            cout<<"Enter Temperature in Fahrenheit: ";
            cin>>ut;
            ct= (ut-32)/1.8;
            cout<<ct<<" C";
        }
        if (ch==2)
        {
            cout<<"Enter Temperature in Celsius: ";
            cin>>ut;
            ct=(1.8*ut)+32;
            cout<<ct<<" F";
        }
        return 0; 
    }

    Output:

    Enter 1 to change temperature from Fahrenheit to Celsius
    Enter 2 to change temperature from Celsius to Fahrenheit
    2
    Enter Temperature in Celsius: 12
    53.6 F

    Python Program to Convert Fahrenheit into Celsius

    print("Enter 1 to change temperature from Fahrenheit to Celsius")
    print("Enter 2 to change temperature from Celsius to Fahrenheit")
    ch = int(input(""))
    if ch==1:
        ut= float(input("Enter Temperature in Fahrenheit: " ))
        ct=(ut-32)/1.8
        print(ct,"C")
    if ch==2:
        ut= float(input("Enter Temperature in Celsius: " ))
        ct=(1.8*ut)+32
        print(ct,"F")

    Output:

    Enter 1 to change temperature from Fahrenheit to Celsius
    Enter 2 to change temperature from Celsius to Fahrenheit
    1
    Enter Temperature in Fahrenheit: 23
    -5.0 C

    Java Program to Convert Fahrenheit into Celsius

    import java.util.Scanner;
    public class Main
    {
        public static void main(String[] args)
        {
            double ut,ct, ch;
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter 1 to change temperature from Fahrenheit to Celsius\n");
            System.out.println("Enter 2 to change temperature from Celsius to Fahrenheit\n");
            ch = sc.nextDouble();
            if (ch==1)
            {
                System.out.println("Enter Temperature in Fahrenheit: ");
                ut = sc.nextDouble();
                ct= (ut-32)/1.8;
                System.out.println(+ct+" C");
            }
            if (ch==2)
            {
                System.out.println("Enter Temperature in Celsius: ");
                ut = sc.nextDouble();
                ct=(1.8*ut)+32;
                System.out.println(+ct+" F");
            }
        }
    }

    Output:

    Enter 1 to change temperature from Fahrenheit to Celsius
    
    Enter 2 to change temperature from Celsius to Fahrenheit
    
    2
    Enter Temperature in Celsius: 
    45
    113.0 F

    Conclusion

    This was all about writing a program in C, C++, Python, and Java to convert Fahrenheit into Celsius and vice versa. You just need to know the right formulas and how to use the if statement. You can easily write the above programs without any hassle.

    If you face any difficulties while writing the code, let us know in the comments.

    People are also reading:

    Leave a Comment on this Post

    0 Comments