Buch Cover Buch Cover Buch Cover Buch Cover

Web-Code: - Webcode Help

Hundert Binärzahlen (Selektionen)

Schreiben Sie ein Programm, das eine Zahl zwischen 0 und 100 entgegennimmt und diese Zahl im Binärsystem ausgibt.

0 Kommentare

Bitte melde dich an um einen Kommentar abzugeben

6 Lösung(en)

/* Wäre auch mit einer Schleife lösbar, diese wurde aber noch nicht behandelt. Autor: phi@gressly.ch 2009*/ 

 void start() {
    int input = einlesen("Zahl von 0 bis 99");
    String resultat = "";
    // Einer:
    if(0 == input % 2) { // gerade?
      resultat = "0"; }
    else {
      resultat = "1";   }
    //Zweier:
    input = input / 2; 
    if(0 == input % 2) { // gerade?
        resultat = "0" + resultat; }
      else {
        resultat = "1" + resultat;    }
    //Vierer:
    input = input / 2; 
    if(0 == input % 2) { // gerade?
        resultat = "0" + resultat; }
      else {
        resultat = "1" + resultat;    }
    //Achter:
    input = input / 2; 
    if(0 == input % 2) { // gerade?
        resultat = "0" + resultat; }
      else {
        resultat = "1" + resultat;    }
    //Sechzehner:
    input = input / 2; 
    if(0 == input % 2) { // gerade?
        resultat = "0" + resultat; }
      else {
        resultat = "1" + resultat;    }
    //Zweiunddreissiger:
    input = input / 2; 
    if(0 == input % 2) { // gerade?
        resultat = "0" + resultat; }
      else {
        resultat = "1" + resultat;    }
    //Vierundsechziger:
    input = input / 2; 
    if(0 == input % 2) { // gerade?
        resultat = "0" + resultat; }
      else {
        resultat = "1" + resultat;    }
 
   System.out.println(resultat); }

  int einlesen(String bezeichnung) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Geben Sie " + bezeichnung + " ein: ");
    return sc.nextInt();  }
                
{$R+}
{$B+}
program BinaerAusgabe(input,output);
{Eingabe n natuerliche Zahl ausgabe Binaer}
{FernUni Hagen}

const
INDEX = 7;

type
tIndex = 1..INDEX;
tFeld = array[tIndex] of integer;

var
Eingabe : integer;
Feld : tFeld;
Lauf : tIndex;
begin
  Eingabe := 0;
  write('Geben Sie eine Zahl bis 100 ein: ');
  readln(Eingabe);
  if (Eingabe >100) then
  begin
    writeln('falsche Eingabe!');
  end
  else
  if ( (Eingabe - 64) >= 0 ) then
  begin
    Eingabe := Eingabe - 64;
    Feld[1] := 1;
  end
  else
    Feld[1] := 0;
  if ( (Eingabe - 32) >= 0 ) then
  begin
    Eingabe := Eingabe - 32;
    Feld[2] := 1;
  end
  else
    Feld[2] := 0;
  if ( (Eingabe - 16) >= 0 ) then
  begin
    Eingabe := Eingabe - 16;
    Feld[3] := 1;
  end
  else
   Feld[3] := 0;
  if ( (Eingabe - 8) >= 0 ) then
  begin
    Eingabe := Eingabe - 8;
    Feld[4] := 1;
  end
  else
   Feld[4] := 0;
  if ( (Eingabe - 4) >= 0 ) then
  begin
    Eingabe := Eingabe - 4;
    Feld[5] := 1;
  end
  else
   Feld[5] := 0;
  if ( (Eingabe - 2) >= 0 ) then
  begin
    Eingabe := Eingabe - 2;
    Feld[6] := 1;
  end
  else
    Feld[6] := 0;
  if ( (Eingabe - 1) >= 0 ) then
  begin
    Eingabe := Eingabe - 1;
    Feld[7] := 1;
  end
  else
    Feld[7] := 0;
  {Ausgabe}
  for Lauf:=1 to INDEX do
  begin
    write(Feld[Lauf], ' ');
  end;
  readln();
end.
                

Lösung von: Name nicht veröffentlicht

{$R+}
{$B+}

program Binaerzahlen (input, output);
{ rechnet eine Zahl zwischen 0 und 100 in eine Binaerzahl um }

	 const
	 INDEX = 7;
	 
	 type
	 tIndex = 1..Index;
	 tFeld = array[tIndex] of integer;
	 
	 var
	 Zahl,
	 Rest : integer;
	 Feld : tFeld;
	 i : integer;
	 

begin
	writeln('Bitte geben Sie eine Zahl zwischen 1 und 100 ein: ');
	readln(Zahl);
	writeln('Die Binaerzahl zu ', Zahl, ' lautet: ');
	i:= 7;
	while (Zahl <> 0) do
	begin
		Rest := Zahl mod 2;
		Zahl := Zahl div 2;
		Feld[i] := Rest;
		i := i-1
	end;
	for i := 1 to INDEX do
		write(Feld[i], ' ')
end. { Binaerzahlen }

                

Lösung von: Katja Rummler ()

package programmierenlernen.kapitel3;

import java.util.Scanner;

public class Aufgabe_3_8 {

	public static void main(String[] args) {
		new Aufgabe_3_8().top();
	}

	void top() {
		
		int zahleingabe = eingabeInt("Geben Sie eine Zahl ein:");
		
		while(zahleingabe < 0 || zahleingabe > 100) {
			zahleingabe = eingabeInt("Geben Sie eine Zahl zwischen 0 und 100 ein:");
		}
		
		print("Binärzahl: " + Integer.toBinaryString(zahleingabe));
		
	}
	
	Scanner sc = new Scanner(System.in);
	int eingabeInt(String textstring) {
		print(textstring);
		return sc.nextInt();
	}
	
	void print(String textstring) {
		System.out.println(textstring);
	}

}
                

Lösung von: Jan Roth (Santis Training AG)

console.log(Math.floor(Math.random() * 100).toString(2));
                

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

// C++ 14 | VS-2022
#include <iostream>
#include <string>
#include <bitset>

// Variante 1 - klassisch
const auto to_binary_1(int n) {
    std::string str{};
    while (n) {
        str = std::to_string(n % 2) + str;
        n /= 2;
    }
    return str;
}

// Variante 2 - mit Bordmitteln
const auto to_binary_2(int n) {
    return std::bitset<8>(n).to_string();
}

int main() {
    srand(time(nullptr));
    auto n{ rand() % 101 };
    std::cout << n << " -> " << to_binary_1(n) << "\n";
    std::cout << n << " -> " << to_binary_2(n) << "\n";
}
                

Lösung von: Jens Kelm (@JKooP)

Verifikation/Checksumme:

  • 3 -> 000 0011
  • 5 -> 000 0101
  • 10 -> 000 1010
  • 80 -> 101 0000

 

Aktionen

Bewertung

Durchschnittliche Bewertung:

Eigene Bewertung:
Bitte zuerst anmelden

Meta

Zeit: 1
Schwierigkeit: k.A.
Webcode: an5r-qqzp
Autor: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)

Download PDF

Download ZIP

Zu Aufgabenblatt hinzufügen