Skip to content
Related Articles

Related Articles

Program for Celsius To Fahrenheit conversion

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 24 May, 2022
View Discussion
Improve Article
Save Article

Given a Temperature ‘n’ in Celsius scale, your task is to convert it into Fahrenheit scale.
Examples: 

Input : 0
Output : 32

Input : -40
Output : -40

Formula for converting Celsius scale to Fahrenheit scale 

 T(°F) = T(°C) × 9/5 + 32

C




/* Program to convert temperature from Celsius to Fahrenheit*/
#include <stdio.h>
 
//function for conversion
float celsius_to_fahrenheit(float c){
 return ((c * 9.0 / 5.0) + 32.0);
}
 
int main() {
 
    float c = 20.0;
   
     // passing parameter to function and printing the value
      printf("Temperature in Fahrenheit : %0.2f",celsius_to_fahrenheit(c));
    return 0;
}


C++




// CPP program to convert Celsius
// scale  to Fahrenheit scale
#include <bits/stdc++.h>
using namespace std;
 
// function to convert Celsius
// scale to Fahrenheit scale
float Cel_To_Fah(float n)
{
    return ((n * 9.0 / 5.0) + 32.0);
}
 
// driver code
int main()
{
    float n = 20.0;
    cout << Cel_To_Fah(n);
    return 0;
}


Java




// Java program to convert Celsius
// scale to Fahrenheit scale
class GFG
{
// function to convert Celsius
// scale to Fahrenheit scale
static float Cel_To_Fah(float n)
{
    return ((n * 9.0f / 5.0f) + 32.0f);
}
 
// Driver code
public static void main(String[] args) {
    float n = 20.0f;
    System.out.println(Cel_To_Fah(n));
}
}
 
// This code is contributed by Anant Agarwal.


Python3




# Python code to convert Celsius scale
# to Fahrenheit scale
def Cel_To_Fah(n):
     
    # Used the formula
    return (n*1.8)+32
 
# Driver Code
n = 20
print(int(Cel_To_Fah(n)))
 
# This code is contributed by Chinmoy Lenka


C#




// C# program to convert Celsius
// scale to Fahrenheit scale
using System;
 
class GFG  {
     
// function to convert Celsius
// scale to Fahrenheit scale
static float Cel_To_Fah(float n)
{
    return ((n * 9.0f / 5.0f) + 32.0f);
}
 
// Driver code
public static void Main()
{
    float n = 20.0f;
    Console.Write(Cel_To_Fah(n));
}
}
 
// This code is contributed by Nitin Mittal.


PHP




<?php
// PHP program to convert Celsius
// scale to Fahrenheit scale
 
// function to convert Celsius
// scale to Fahrenheit scale
function Cel_To_Fah($n)
{
    return (($n * 9.0 / 5.0) + 32.0);
}
 
    // Driver Code
    $n = 20.0;
    echo Cel_To_Fah($n);
     
// This code is contributed by nitin mittal
?>


Javascript




<script>
 
// Javascript program to convert Celsius
// scale to Fahrenheit scale
 
// function to convert Celsius
// scale to Fahrenheit scale
function Cel_To_Fah(n)
{
    return ((n * 9.0 / 5.0) + 32.0);
}
 
// driver code
 
    let n = 20.0;
    document.write(Cel_To_Fah(n));
 
 
// This code is contributed by Mayank Tyagi
 
</script>


Output: 

68

Time Complexity: O(1), as we are not using any loops.

Auxiliary Space: O(1), as we are not using any extra space.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!