Python/C/C++/JAVA

Basic Practice Programs with Code and Concept

By D.S

Convert the Temperature

If you're looking to write a program to convert temperature in different programming languages like Java, C, C++, and Python, then you've come to the right place. Essentially, the formula for temperature conversion remains constant across every language - it's all about choosing the right variable types and knowing the correct syntax in each language. In Java, for example, one approach could be to create two separate methods for converting Celsius to Fahrenheit and vice versa while using double data type variables. Meanwhile, in C++, you would need to declare variables using float or double names and utilize appropriate control structures like loops and conditional statements. Similarly, Python has its own methods that let you easily convert temperature values too! The key is just finding your comfort zone with each language's distinct requirements and syntax – happy coding!

(a.) C Code

#include <stdio.h>
    int main() {
        float celsius, fahrenheit;

        printf("Enter temperature in Celsius: ");
        scanf("%f", &celsius);

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

        printf("Temperature in Fahrenheit: %.2f
", fahrenheit);

        return 0;
    }
Output:-
Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77.00

(b.) C++ Code

#include <iostream>
    using namespace std;

    int main() {
        float celsius, fahrenheit;

        cout << "Enter temperature in Celsius: ";
        cin >> celsius;

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

        cout << "Temperature in Fahrenheit: " << fixed << fahrenheit << endl;

        return 0;
    }
Output:-
Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77.00

(c.) Python Code

def main():
        celsius = float(input("Enter temperature in Celsius: "))
        fahrenheit = (celsius * 9 / 5) + 32
        print("Temperature in Fahrenheit: {:.2f}".format(fahrenheit))

    if __name__ == "__main__":
        main()
Output:-
Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77.00

(d.) Java Code

import java.util.Scanner;

    public class TemperatureConverter {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter temperature in Celsius: ");
            float celsius = scanner.nextFloat();
            float fahrenheit = (celsius * 9 / 5) + 32;
            System.out.println("Temperature in Fahrenheit: " + fahrenheit);
        }
    }
Output:-
Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77.00