Write a program to convert Fahrenheit to Celcius.
Example
Input: Fehrenheit= 37.76
Output: 3.2
Approach
Java
public class FahrenheitToCelsius {public static void main(String[] args) {double fahrenheit = 37.76;double celsius = fahToCel(fahrenheit);System.out.printf("Celsius is %.2f", celsius);}private static double fahToCel(double fahrenheit) {// Formula : ((f + 40) ÷ 1.8) − 40 = c.double celsius = ((fahrenheit + 40) / 1.8) - 40;return celsius;}}
C++
#include <bits/stdc++.h>using namespace std;//function to convert the//fahrenheit to celciusdouble fahrenheitToCelcius(double fahrenheit){//formula: ((f+40)/1.8)-40=cdouble celcius=((fahrenheit+40)/1.8)-40;return celcius;}int main(){double fahrenheit=37.76;double celcius=fahrenheitToCelcius(fahrenheit);cout<<"Celcius is ";cout<<celcius<<"\n";return 0;}
No comments:
Post a Comment