Multiplizieren ohne Multiplizieren (Schleifen)
Versucht eine Multiplikationsaufgabe mit 3 Faktoren (x * y * z) zu lösen, ohne die Multiplikation ihrer Programmiersprache zu verwenden.
Mann sollte für x,y und z beliebige positive ganze Zahlen einsetzten können.
0 Kommentare
21 Lösung(en)
- java
- java
- generic
- c
- java
- python
- javascript
- java
- c
- cpp
- csharp
- vb
- generic
- csharp
- abap
- java
- java
- python
- javascript
- csharp
- csharp
public class Multiplicator {
private Scanner sc;
public static void main(String[] args) {
new Multiplicator().load();
}
public void load() {
System.out.println("\nErgebnis: " + calc());
}
private int calc() {
int first = getFirstMultiplicator();
int second = getSecondMultiplicator();
int third = getThirdMultiplicator();
int calc = 0;
for(int i = 1; i <= first; i++) {
calc += second;
}
first = calc;
calc = 0;
for(int i = 1; i <= first; i++) {
calc += third;
}
return calc;
}
private int getFirstMultiplicator() {
try {
sc = new Scanner(System.in);
System.out.print("Geben Sie den ersten Multiplikator ein: ");
String value = sc.next();
return Integer.parseInt(value);
} catch (NumberFormatException e) {
System.out.println("Bitte geben Sie nur Zahlen ein!");
System.exit(0);
}
return 0;
}
private int getSecondMultiplicator() {
try {
System.out.print("Geben Sie den zweiten Multiplikator ein: ");
String value = sc.next();
return Integer.parseInt(value);
} catch (NumberFormatException e) {
System.out.println("Bitte geben Sie nur Zahlen ein!");
System.exit(0);
}
return 0;
}
private int getThirdMultiplicator() {
try {
System.out.print("Geben Sie den dritten Multiplikator ein: ");
String value = sc.next();
sc.close();
return Integer.parseInt(value);
} catch (NumberFormatException e) {
System.out.println("Bitte geben Sie nur Zahlen ein!");
System.exit(0);
}
return 0;
}
}
Lösung von: Name nicht veröffentlicht
import java.util.InputMismatchException;
import java.util.Scanner;
public class MultiplizierenOhneMultiplizieren {
public static int leseInt(String i) {
Scanner scan = new Scanner(System.in);
int zahl2 = 0;
System.out.println("Geben Sie " + i + " ein (Es muß eine Zahl sein)");
try {
zahl2 = scan.nextInt();
} catch (InputMismatchException e) {
System.out.println("Du hast keine Ganzzahl eingegeben!");
zahl2 = leseInt(i);
}
return zahl2;
}
public static void main(String[] args) {
System.out.println("Bitte gebe deine 3 Zahlen ein");
int x = leseInt("x");
int y = leseInt("y");
int z = leseInt("z");
int erg = 0;
for (int i = 1; i <= y; i++) {
for (int j = 1; j <= z; j++) {
erg = erg + x;
}
}
System.out.println(erg);
}
}
Lösung von: Daniel Majunke (GFN)
'24.3.2017 - Powerbasic 10
#COMPILE EXE
#DIM ALL
FUNCTION PBMAIN () AS LONG
DIM x AS LONG
DIM y AS LONG
DIM z AS LONG
DIM inpStr AS STRING
inpStr = INPUTBOX$("Bitte geben Sie 3 kommagetrennte positive Ganzzahlen zum Multiplizieren ein:","Multiplizieren","50,213,22")
x = VAL(PARSE$(inpStr,",",1))
y = VAL(PARSE$(inpStr,",",2))
z = VAL(PARSE$(inpStr,",",3))
IF isValid(FORMAT$(x+y+z)) THEN
DIM i AS LONG
DIM t AS LONG
DIM Produkt AS LONG
FOR t = 1 TO z
FOR i = 1 TO y
Produkt += x
NEXT i
NEXT t
MSGBOX STR$(Produkt),,EXE.NAME$
ELSE
MSGBOX "Bitte Eingaben nochmal überdenken....",,EXE.NAME$
END IF
END FUNCTION
'-------------------------
FUNCTION IsValid(aa AS STRING) AS BYTE
IsValid = 0
IF LEN(aa$) AND RETAIN$(aa$,ANY "0123456789")=aa$ AND VAL(aa$) > 0 THEN
IsValid = 1
END IF
END FUNCTION
Lösung von: Markus Sägesser (keine)
#include <stdio.h>
int main(void) {
int anzahlAnFaktoren = 3;
int faktor[anzahlAnFaktoren];
int ergebnis = 0;
int zwErgebnis = 0;
int i;
int j;
for (i = 0; i <= anzahlAnFaktoren - 1; i++) {
scanf("%i", &faktor[i]);
}
ergebnis = faktor[0];
printf("%i * %i * %i = ", faktor[0], faktor[1], faktor[2]);
for (j=0; j< anzahlAnFaktoren - 1; j++) {
for (i = 0; i < ergebnis; i++) {
zwErgebnis += faktor[j + 1];
}
ergebnis = zwErgebnis;
zwErgebnis = 0;
}
printf("%i \n", ergebnis);
return 0;
}
Lösung von: Julian Akkaya (-)
public class Hauptklasse
{
public static void main(String[] args)
{
java.util.Scanner scanner = new java.util.Scanner(System.in);
System.out.print("Erster Faktor: ");
int Faktor1 = scanner.nextInt();
System.out.print("Zweiter Faktor: ");
int Faktor2 = scanner.nextInt();
System.out.print("Dritter Faktor: ");
int Faktor3 = scanner.nextInt();
int zaehler1 = 1;
int zaehler2 = 1;
int Zwischenergebnis = 0;
while (zaehler1 <= Faktor3)
{
zaehler2 = 1;
while (zaehler2 <= Faktor2)
{
Zwischenergebnis += Faktor1;
zaehler2 += 1;
}
zaehler1 += 1;
}
System.out.print("Ergebnis: " + Zwischenergebnis);
}
}
Lösung von: Arnim Pankratz (Freiherr-vom-Stein-Gymnasium Betzdorf)
def mult(a, b, c):
prod = 0
for _ in range(a):
for __ in range(b):
prod += c
return prod
Lösung von: Bester Mensch (Class)
var x = 2;
var y = 3;
var z = 4;
var a = 0;
for (var i = 1; i <= y; i++) {
for (var j = 1; j <= z; j++) {
console.log(a += x);
}
}
Lösung von: Irene Strauß (Liceo Scientifico, Bruneck)
package Aufgaben.Multiply;
public class MultiplyWithoutMultiplying
{
public static void main(String[] args)
{
System.out.println(multiply(3,multiply(4,8)));
}
private static int multiply(int a, int b)
{
int result = 0;
for(int i = 0; i < b; i++)
{
result += a;
}
return result;
}
}
Lösung von: Name nicht veröffentlicht
#include <stdio.h>
int mult(int a, int b){
return (a == 0 || b == 0 ? 0 : mult(a-1,b) + b);
}
int mult2(int a, int b, int c){
return mult(mult(a,b),c);
}
int main(int argc, char *argv[]){
int i=0;
char check;
int fac[3];
for (i=0; i<3; i++){
do {
printf("Geben Sie den %d. Faktor ein: ", i+1);
check = scanf("%d",&fac[i]);
}
while(fac[i] < 0 || getchar() != '\n' || check != 1);
}
printf("Res = %d\n",mult2(fac[0],fac[1],fac[2]) );
}
Lösung von: André Trobisch ()
/*
* File: Multiplizieren ohne Multiplizieren
* Author: Manuel Stolze
* Created on 23. Juni 2017, 10:55
*/
#include <cstdlib>
#include <iostream>
using namespace std;
class Berechnung {
public:
void berechnen();
private:
int x = 0;
int y = 0;
int z = 0;
int ergebnis = 0;
int counter1 = 1;
int counter2 = 1;
};
void Berechnung::berechnen(){
// Werte einlesen //
cout << "Geben Sie die erste Zahl ein: ";
cin >> x;
cout << "Geben Sie die zweite Zahl ein: ";
cin >> y;
cout << "Geben Sie die dritte Zahl ein: ";
cin >> z;
// Multiplikation 3. Zahl //
while(counter1 <= z){
counter2 = 1;
// Multiplikation 2. Zahl //
while(counter2 <= y){
ergebnis += x;
counter2 += 1;
}
counter1 += 1;
}
cout << "Das Ergebnis ist: " << ergebnis;
}
int main(int argc, char** argv) {
Berechnung berechnung;
berechnung.berechnen();
return 0;
}
Lösung von: Manuel Stolze (Hochschule Darmstadt)
int zahl1 ;
int zahl2 ;
int zahl3 ;
//Eingabe der Werte
do {
Console.Write("Zahl 1 ");
string zahll1 = Console.ReadLine();
bool versuch = int.TryParse(zahll1, out zahl1);
if (zahl1 < 0)
{
Console.WriteLine("Die Zahl muss größer als Null sein" +"\n");
}
if (!int.TryParse(zahll1, out zahl1))
{
Console.WriteLine("Nur die Eingabe von Zahlen ist gestattet." +"\n");
zahl1 = -1;
}
} while(zahl1<0);
do
{
Console.Write("Zahl 2 ");
string zahll2 = Console.ReadLine();
bool versuch = int.TryParse(zahll2, out zahl2);
if (zahl2 < 0)
{
Console.WriteLine("Die Zahl muss größer als Null sein" + "\n");
}
if (!int.TryParse(zahll2, out zahl2))
{
Console.WriteLine("Nur die Eingabe von Zahlen ist gestattet." + "\n");
zahl2 = -1;
}
} while (zahl2 < 0);
do
{
Console.Write("Zahl 3 ");
string zahll3 = Console.ReadLine();
bool versuch = int.TryParse(zahll3, out zahl3);
if (zahl3 < 0)
{
Console.WriteLine("Die Zahl muss größer als Null sein" + "\n");
}
if (!int.TryParse(zahll3, out zahl3))
{
Console.WriteLine("Nur die Eingabe von Zahlen ist gestattet." + "\n");
zahl3 = -1;
}
} while (zahl3 < 0);
//Platzhaltervariablen für Zwischenergebnisse
int k = 0;
int l = k;
//erster Teiler der Multiplikation
int zähler = 1;
while (zähler <= zahl2) //EIngabe Zahl2
{
zähler++;
k = k + zahl1;
}
//zweiter Teil der Multiplikation
zähler = 1;
while (zähler <= zahl3) //EIngabe Zahl3
{
zähler++;
l = l + k;
}
//Ausgabe Ergebnis
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.WriteLine(l );
Console.ResetColor();
}
Lösung von: Goo Muck (Uni)
Sub Main()
'Eingabe
Console.Write("X: ")
Dim x As Integer = Console.ReadLine()
Console.Write("Y: ")
Dim y As Integer = Console.ReadLine()
Console.Write("Z: ")
Dim z As Integer = Console.ReadLine()
'Berechnen
Dim summe As Integer = 0
For i As Integer = 1 To z
Dim summex As Integer = 0
For j As Integer = 1 To x
summex += y
Next
summe += summex
Next
'Ausgabe
Console.WriteLine("Ergebniss: " & summe)
Console.ReadLine()
End Sub
Lösung von: Elias Zech (Optics Balzers)
//Lösung in Go
package main
import "fmt"
var x int = 5
var y int = 8
var z int = 10
var produkt int = 0
func main() {
for i := 1; i <= x; i++ {
for j := 1; j <= y; j++ {
produkt += z
}
}
fmt.Println(produkt)
}
Lösung von: Name nicht veröffentlicht
using System;
using System.Linq;
namespace Multiplikator
{
class Program
{
static void Main(string[] args)
{
//Nutzer Eingabe
Console.WriteLine("Gebe die Multiplikationsaufgabe ein:");
string[] Aufgabe = Console.ReadLine().Replace(" ", String.Empty).Split('*');
int Wert = 0;
int Zwischenwert = Int32.Parse(Aufgabe[0]);
//Schleife die jeden außer den ersten Wert in die Multiplikation einbezieht.
for (int i = 1; i < Aufgabe.Length; i++)
{
//Zwischenwert = Wert setzen, damit mehr als 2 Faktoren verwendet werden können
if (Wert != 0)
{
Zwischenwert = Wert;
Wert = 0;
}
//Für jede Multiplikation den Zwischenwert draufrechnen
for(int k = 0; k < Int32.Parse(Aufgabe[i]); k++)
{
Wert += Zwischenwert;
}
}
Console.WriteLine("Das Ergebnis ist: " + Wert);
Console.ReadLine();
}
}
}
Lösung von: Tobias Golz (Wilhelm Büchner Hochschule)
REPORT Z_MULT_OHNE_MULT.
parameters: x type i OBLIGATORY,
y type i OBLIGATORY,
z type i OBLIGATORY.
perform mult using x y z.
form mult using value(a) type i value(b) type i value(c) type i.
data i like c.
i = 0.
do a times.
do b times.
i = i + c.
enddo.
enddo.
write: a, 'mal', b, 'mal', c, 'ist', i.
endform.
Lösung von: Name nicht veröffentlicht
//Lösung mit Klassen
package dreizahlenmul;
import java.util.Random;
import java.util.Scanner;
import dreizahlenmul.mal.*;
class Main {
public static void main(String[] args) {
Mal mal1 = new Mal();
int x, y, z;
Scanner input = new Scanner(System.in);
x = input.nextInt();
y = input.nextInt();
z = input.nextInt();
mal1.multiplikation(x, y, z);
System.out.println(mal1.multiplikation(x, y, z));
}
}
//Klasse
package dreizahlenmul.mal;
public class Mal {
int x, y, z;
public Mal(){
this.x = x;
this.y = y;
this.z = z;
}
public int multiplikation(int x, int y, int z){
int mal = 0;
int mal1 = 0;
for(int i = 0; i < y; i++){
mal += x;
}
for(int j = 0; j < z; j++){
mal1 += mal;
}
return mal1;
}
}
Lösung von: Name nicht veröffentlicht
public class Caluclator {
//Vorbereitung durch Initialisierung der Variablen
int one = 0;
int two = 0;
int three = 0;
//Oeffentliche Methode mit Rückgabewert int und drei Parametern vom Typ int
//Return-Wert ist der Returnwert der Methode multiplicate
public int calculate(int one, int two, int three){
this.one = one;
this.two = two;
this.three = three;
return multiplicate();
}
//Methode multiplicate funktioniert folgendermassen:
//da keine Multiplikationen erlaubt sind, arbeitet man
//mit einer doppelten Schleife, dh:
//2*3*2 => man durchläuft 2 mal die 3-fache Schleife, in welcher
//jeweils der dritte int dem Resultat zugewiesen wird
private int multiplicate(){
int result = 0;
for(int i = 0; i < one; i++ ){
for(int y = 0; y < two; y++){
result += three;
}
}
return result;
}
}
Lösung von: Zeno Rauch (Berufsbildung Baden & kantonsschule Baden)
x = int(input("x: "))
y = int(input("y: "))
z = int(input("z: "))
print("x * y * z =", x/(1/y)/(1/z))
print("Probe: x * y * z =", x * y * z)
Lösung von: Peter Pan (Home Office)
// kern der aufgabe:
// dividieren durch den kehwert
function multiplyWithoutMultiplying(a, b) {
return a / (1 / b);
}
// funktion für beliebig viele parameter
function multiply() {
let arr = [];
for (let i = 0; i < arguments.length; i++) arr.push(arguments[i]);
while (arr.length > 1) {
arr[0] = multiplyWithoutMultiplying(arr[0], arr[1]);
arr.splice(1, 1);
}
return arr[0];
}
console.log(multiply(2, 3));
console.log(multiply(2, 3, 4));
console.log(multiply(2, 3, 4, 5)); // lissalanda@gmx.at
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
// NET Core 3.x; C# 8.x
using System;
using System.Linq;
namespace CS_Aufgabe_Multiplizieren
{
class Program
{
static void Main(string[] args)
{
int x = 3, y = 4, z = 5;
var r = Enumerable.Range(0, Enumerable.Range(0, x).Aggregate(0, (a, v) => a + y)).Aggregate(0, (b, v) => b + z);
Console.WriteLine(r);
}
}
}
Lösung von: Jens Kelm (@JKooP)
using System;
// Programmieraufgaben.ch Aufgaben-Code: ex4w-hzu4
namespace Multiplizieren
{
class Program
{
private static void Main()
{
Console.Write("Wieviele Zahlen sollen multipliziert werden? ");
int config = int.TryParse(Console.ReadLine(), out config) && config > 0 ? config : 2;
int tempresult = 1;
int[] numbers = new int[config + 1];
// Eingabe der Zahlen
for (int runs = 1; runs <= config; runs++)
{
Console.Clear();
Console.Write($"Geben Sie die {runs}. Ganzzahl ein: ");
numbers[runs] = int.TryParse(Console.ReadLine(), out numbers[runs]) ? numbers[runs] : 0;
tempresult = Multi(tempresult, numbers[runs]);
}
// Ausgabe
Console.Clear();
Console.Write($"Das Ergebnis ist: {tempresult}");
Console.ReadKey();
}
private static int Multi(int numOne, int numTwo)
{
// Berechnung Zahlen
int result = 0;
bool negOne = numOne < 0;
bool negTwo = numTwo < 0;
// Negativ-Ausgleich
numOne = negOne ? -numOne : numOne;
numTwo = negTwo ? -numTwo : numTwo;
for (int runs = 1; runs <= numTwo; runs++)
{
result += numOne;
}
// Negativ-Ausgleich
result = negOne ? -result : result;
result = negTwo ? -result : result;
return result;
}
}
}
Lösung von: Howard Dennis King (IT-Schule Stuttgart)
Verifikation/Checksumme:
für x, y bzw. z:
3, 4, 5 → 60
6, 0, 4 → 0
Aktionen
Neue Lösung hinzufügen
Bewertung
Durchschnittliche Bewertung: