Buch Cover Buch Cover Buch Cover Buch Cover

Web-Code: - Webcode Help

Fahrenheit (Schleifen)

Schreiben Sie ein Programm, das von -20 bis 100 Grad die Celsiuswerte in 5er-Schritten in Fahrenheit umrechnet und als Tabelle ausgibt.

Die Formel dazu lautet: fahrenheit = (9.0/5.0) * celsius + 32.

Zusatzaufgabe: Schreiben Sie auch die Umkehrung.

0 Kommentare

Bitte melde dich an um einen Kommentar abzugeben

8 Lösung(en)

public class Main {

    public static void main(String[] args) {
        System.out.println( "---- Celsius -> Fahrenheit ----" );
        for( int i = -20; i <= 100; i += 5 )
            System.out.println( i + " °C = " + fahrenheit(i) + " °F" );

        System.out.println( "---- Fahrenheit -> Celsius ----" );
        for( int i = -20; i <= 100; i += 5 )
            System.out.println( i + " °F = " + celsius(i) + " °C" );
    }
    
    public static double fahrenheit( double celsius ) {
        return( 9./5.*celsius + 32. );
    }

    public static double celsius( double fahrenheit ) {
        return( 5./9.*(fahrenheit-32) );
    }
}

                
print "Celsius", "\t", "Fahreheit"
for i in range (-20, 101, 5):
    print i, "\t"*2, int((9.0/5.0)*i +32)
                

Lösung von: Name nicht veröffentlicht

 /*********************************************************************/
 /* Autor : philipp gressly freimann (@ http://www.sanits-training.ch)*/
 /* Datum : 16. Nov. 2011                                             */
 /* Aufgabe 4.4 (Programmieraufgaben.ch: Fahrenheit)                  */
 /*********************************************************************/

 Fahrenheit : proc options(main noexecops);

  dcl Celsius    bin float(53)    ; /* -20 bis 100 */
  dcl Fahrenheit bin float(53)    ; /* Umgerechnet */

  do Celsius = -20 to 100 by 5;
    Fahrenheit = (9.0 / 5.0) * Celsius + 32.0;
    put
      edit("Celsius: ", Celsius, " => Fahrenheit: ", Fahrenheit)
          (a(10), p'----9V.99',
           a(17), p'----9V.99');
    put skip list('');
  end;

  /* Umkehrung */
  do Fahrenheit = -20 to 100 by 5;
    Celsius = (Fahrenheit - 32) * 5.0 / 9.0;
    put
      edit("Fahrenheit: ", Fahrenheit, " => Celsius: ", Celsius)
          (a(14), p'----9V.99',
           a(15), p'----9V.99');
    put skip list('');
  end;

 end Fahrenheit;
                

Lösung von: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)

program Fahrenheit (input, output);
{ Rechnet Celsius in Fahrenheit um }

	var
	celsius : integer;
	fahrenh : real;
	
	
begin
	celsius := -20;
	while (celsius <= 100) do
	begin
		fahrenh := (9.0/5.0) * celsius + 32;
		writeln('celsius: ', celsius: 2, ' Fahrenheit: ', fahrenh:2:2);
		celsius := celsius + 5
	end
end. { Fahrenheit }
                

Lösung von: Katja Rummler ()

// °c to °f
document.write(`
  <div><table>
  <tr><th>° C</th><th>° F</th></tr>`);

for (let i = -20; i <= 100; i += 5) {
  document.write(`<tr><td>${i}</td><td>${1.8 * i + 32}</td></td></tr>`);
}

document.write('</table></div>');

// fc to °c
document.write(`
  <div><table>
  <tr><th>° F</th><th>° C</th></tr>`);

for (let i = -20; i <= 100; i += 5) {
  document.write(`<tr><td>${i}</td><td>${Math.round((i - 32) / 1.8)}</td></td></tr>`);
}

document.write('</table></div>');
                

Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)

// NET 6.x | C# 10.x | VS-2022


for (int i = -20; i <= 100; i+=5)
{
    // Celsius -> Fahrenheit & Kelvin
    var conCF = new Converter { Celsius = i };
    Console.WriteLine($"C: {i,6:F1}\tF: {conCF.Fahrenheit,6:F1}\tK: {conCF.Kelvin,6:F1}");
    // Fahrenheit -> Celsius & Kelvin
    var conFC = new Converter { Fahrenheit = i };
    Console.WriteLine($"F: {i,6:F1}\tC: {conFC.Celsius,6:F1}\tK: {conFC.Kelvin,6:F1}");
    Console.WriteLine();
}

class Converter
{
    private double _celsius;

    public double Celsius
    {
        get => Math.Round(_celsius, 2);
        set => _celsius = value;
    }

    public double Fahrenheit
    {
        get => Math.Round(_celsius * 1.8 + 32, 2);
        set => _celsius = (value - 32) * (5.0 / 9.0);
    }

    public double Kelvin
    {
        get => Math.Round(_celsius + 273.15, 2);
        set => _celsius = value - 273.15;
    }
}
                

Lösung von: Jens Kelm (@JKooP)

#include "converter.h"
#include <iostream>
#include <iomanip>

// main
int main() {
    std::cout << std::fixed << std::setprecision(2);
    for (auto i = -20; i <= 100; i+=5) {
        converter con{};
        con.set_celsius(i);
        std::cout << "C: " << con.get_celsius() << std::setw(25) << "\tF: " << con.get_fahrenheit() << std::setw(25) << "\tK: " << con.get_kelvin() << std::endl;
    }
}

// converter.h
#pragma once
class converter
{
private:
	double celsius_;
public:
	double get_celsius();
	void set_celsius(double value);
	double get_fahrenheit();
	void set_fahrenheit(double value);
	double get_kelvin();
	void set_kelvin(double value);
};

// converter.cpp
#include "Converter.h"

double converter::get_celsius() { return celsius_; }
void converter::set_celsius(double value) { celsius_ = value; }
double converter::get_fahrenheit() { return celsius_ * 1.8 + 32; }
void converter::set_fahrenheit(double value) { celsius_ = (value - 32) * (5.0 / 9.0); }
double converter::get_kelvin() { return celsius_ + 273.15; }
void converter::set_kelvin(double value) { celsius_ = value - 273.15; }
                

Lösung von: Jens Kelm (@JKooP)

' als VBA-Funktion

Private celsius_ As Double

Public Property Get Celsius() As Double
    Celsius = WorksheetFunction.Round(celsius_, 2)
End Property

Public Property Let Celsius(ByVal v As Double)
    celsius_ = v
End Property

Public Property Get Fahrenheit() As Double
    Fahrenheit = WorksheetFunction.Round(celsius_ * 1.8 + 32, 2)
End Property

Public Property Let Fahrenheit(ByVal v As Double)
    celsius_ = (v - 32) * (5 / 9)
End Property

Public Property Get Kelvin() As Double
    Kelvin = WorksheetFunction.Round(celsius_ + 273.15, 2)
End Property

Public Property Let Kelvin(ByVal v As Double)
    celsius_ = v - 273.15
End Property

Sub Main()
    For i% = -20 To 100 Step 5
        Celsius = i
        Debug.Print "C: " & i & vbTab & " F: " & Fahrenheit & vbTab & " K: " & Kelvin
    Next
End Sub

                

Lösung von: Jens Kelm (@JKooP)

Verifikation/Checksumme:

  • -20° Celsius = -4° Fahrenheit
  • 0° Celsius = 32° Fahrenheit
  • 100° Celsius = 212° Fahrenheit

Aktionen

Bewertung

Durchschnittliche Bewertung:

Eigene Bewertung:
Bitte zuerst anmelden

Meta

Zeit: 0.5
Schwierigkeit: k.A.
Webcode: kcyy-2mt8
Autor: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)

Download PDF

Download ZIP

Zu Aufgabenblatt hinzufügen