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