Skip to content

Convert Fahrenheit To Celsius

Updated: at 09:04 PM

Table of contents

Open Table of contents

Write a program to convert fahrenheit to celsius

celsius = (fahrenheit - 32) * 5/9

Input ( F )  : -40
Output ( C ) : -40

C

// Write a C program to convert fahrenheit to celsius

#include <stdio.h>

int main()
{

    float celsius, fahrenheit;

    printf("enter fahrenheit \n");
    scanf("%f", &fahrenheit);

    celsius = (fahrenheit - 32) * 5 / 9;

    printf("celsuis is %f", celsius);

    return 0;
}

C#

// Write a C# program to convert fahrenheit to celsius

using System;
class MainClass {
    public static void Main (string[] args) {
    Console.WriteLine ("Please enter the fahrenheit : ");
    double fahrenheit = Convert.ToDouble(Console.ReadLine());
    double celcius =  (fahrenheit - 32) * 5/9;
    Console.WriteLine(fahrenheit+" fahrenheit is "+celcius+" celcius");
  }
}

C++

// Write a C++ program to convert fahrenheit to celsius

#include <iostream>

int main()
{
    // Write C++ code here
    float fahrenheit, celsius;
    std::cout << "enter fahrenhait value : " << std::endl;
    std::cin >> fahrenheit;

    celsius = (fahrenheit - 32) * 5 / 9;

    std::cout << "fahrenheit " << fahrenheit << std::endl;
    std::cout << "celcius :" << celsius;

    return 0;
}

Dart

// Write a Dart program to convert fahrenheit to celsius

import 'dart:io';
void main(){
  print("Enter the Fahrenheit :");
  var fahrenheit =int.parse(stdin.readLineSync());
  var celsius = (fahrenheit - 32) * 5/9;
  print('Equivalent Celsius : $celsius');
}

F#

// Write a F# program to convert fahrenheit to celsius

let fahrenheitToCelsius fahrenheit : float =
   (fahrenheit - 32.0) * 5.0/9.0

let celcius = fahrenheitToCelsius -40.0

printfn "Equivalent celcius : %f" celcius

Go

// Write a Go program to convert fahrenheit to celsius

package main

import "fmt"

func main() {
	var ftemp float64 = 0
	fmt.Scanf("%f", &ftemp)
	ctemp := (ftemp - 32) * 0.556
	fmt.Printf("%.2f", ctemp)
}

Haskell

-- Write a Haskell program to convert fahrenheit to celsius

main :: IO ()
main = do
    putStrLn "Enter temperature in Fahrenheit: "
    input <- getLine
    let fahrenheit = read input :: Float

    let celsius = (fahrenheit - 32) * 5/9
    putStrLn $ "Given Fahrenheit in Celsius: " ++ show(celsius) ++ " °C"

Java

// Write a Java program to convert fahrenheit to celsius

import java.util.Scanner;
public class ConvertFahrenheitToCelsius {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Input (ºF): ");
        int fahrenheit = sc.nextInt();
        sc.close();
        System.out.print("Output (ºC): ");
        int celcius = (fahrenheit - 32) * 5 / 9;
        System.out.println(celcius);
    }
}

// You can use float, double or any other data type according to convinience.

JavaScript

// Write a JavaScript program to convert fahrenheit to celsius

function convertFahrenheitToCelsius(fahrenheit) {
  return ((fahrenheit - 32) * 5) / 9;
}

console.log("Given Fahrenheit in Celsius:", convertFahrenheitToCelsius(50));

Julia

# Write a Julia program to convert fahrenheit to celsius

println("Enter value in Fahrenheit: ")
fahrenheit = readline()
fahrenheit = parse(Float64, fahrenheit)
celsius = round((fahrenheit - 32) * 5/9; digits = 2)
println("Given Fahrenheit in Celsius: ", celsius, " °C")

Kotlin

// Write a Kotlin program to convert fahrenheit to celsius

fun main() {
    print("Enter fahrenheit : ")
    println()
    var input = readLine()
    println("$input fahrenheit = " + ((input!!.toDouble() -32) * 5/9) + " celsius")
}

Perl

# Write a Perl program to convert fahrenheit to celsius

print "Enter the Fahrenheit: ";
my $fahrenheit = <STDIN> ;

my $res = fahrenheitToCelsius($fahrenheit);
print "Fahrenheit to Celsius : $res";

sub fahrenheitToCelsius
{
    my ($x) = @_;
    my $res = ($fahrenheit -32) * 5/9;
    return $res ;
}

PHP

// Write a PHP program to convert fahrenheit to celsius

<?php

function fahrenheit2Celsius($fahrenheit) {
    return round(($fahrenheit - 32) * 5/9, 2);
}

echo fahrenheit2Celsius(-40);

?>

Python

# Write a Python program to convert fahrenheit to celsius

# convert fahrenheit to celsius


def convertFahrenheitToCelsius(fahrenheit):
    return (fahrenheit - 32) * (5 / 9)


if __name__ == "__main__":
    fahrenheit = int(input("give fahrenheit :"))

    print(convertFahrenheitToCelsius(fahrenheit))

R

# Write a R program to convert fahrenheit to celsius

fahrenheit = -40
celsius = (fahrenheit - 32) * 5/9
cat("celsius: ", celsius)

Ruby

# Write a Ruby program to convert fahrenheit to celsius

def convertFahrenheitToCelcius degrees
    puts ((degrees - 32) * 5) / 9
end

convertFahrenheitToCelcius(-40)

Rust

// Write a Rust program to convert fahrenheit to celsius

// Rust function to convert fahrenheit to celsius

use std::io;

fn convert_fahrenheit_to_celsius(fahrin: &str) -> String{

    	let fahrin: f32 = fahrin.trim().parse().expect("Must enter a temperature °C");
    	let celsius = ((fahrin - 32.0) * 5.0) / 9.0;

	let celout = format!("{}", celsius);
	return celout;
}

fn main(){
	// the input string and this main function can be removed. This is simply for testing

	println!("Enter the temperature in fharenheit");
  	let mut fahrenheit = String::new();
	let _rtn = io::stdin().read_line(&mut fahrenheit);

	let celsius = convert_fahrenheit_to_celsius(&fahrenheit);

	// print out the results of the conversion.
	let len = fahrenheit.len();
	fahrenheit.truncate(len - 1);
	println!();
	println!("{}°F is {}°C", fahrenheit, celsius);

}

Scala

// Write a Scala program to convert fahrenheit to celsius

import scala.io.StdIn._;

object fahrenheitToCelsius {
    def main(args:Array[String]):Unit={
        println("Enter the Fahrenheit : ");
        var f = readLine().toInt;
        var celcius = (f-32) * 5/9;
        println("Fahrenheit to Celcius conversion : "+celcius);
    }
}

Swift

// Write a Swift program to convert fahrenheit to celsius

import Swift
import Foundation

print("Enter fahrenheit: ")
var fahrenheit = Int(readLine()!)!
print("Celsius: ", (fahrenheit - 32) * 5/9)

TypeScript

// Write a TypeScript program to convert fahrenheit to celsius

function convertFahrenheitToCelsius(fahrenheit: number): string {
  return (((fahrenheit - 32) * 5) / 9).toFixed(2);
}

console.log("Given Fahrenheit in Celsius:", convertFahrenheitToCelsius(50));

Similar programs