Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Produkt durch Addition berechnen

Empfohlene Antworten

Veröffentlicht

hy hallo erstmal mein Problem ist ich soll

das Produkt zweier Zahlen als addition berechen

so weit so gut kein prob


#include<iostream>


using namespace std;


void main(void)

{ 

	int zahlA;

	int zahlB;

             int proAdd=0;

	int i;

	cout<<"Bitte geben sie Die Erste Zahl ein: ";

	cin>>zahlA;

	cout<<endl<<"Bitte geben sie die zweite Zahl ein: ";

	cin>>zahlB;

	cout<<endl;


	for(i=0;i<zahlB;i++)

	    proAdd=proAdd+zahlA;

          	 if(zahlB==0)

	    proAdd=0;


	cout<<"das ergebniss ist : "<<proAdd;

	cout<<endl;


}

das ist net schlimm was mein problem ist wenn der zweiten zahl bei der eingabe ein minus voran gestellt ist dann

kommt natürlich ein falsches ergebnis raus kann mir da jemand helfen ??

o.k. das stimmt mit dem 0 ist überflüssig

nun aber zum *(-1)

das habe ich auch gemacht


#include<iostream>


using namespace std;


void main(void)

{ 

	int zahlA;

	int zahlB;

    int proAdd=0;

	int i;


	cout<<"Bitte geben sie Die Erste Zahl ein: ";

	cin>>zahlA;

	cout<<endl<<"Bitte geben sie die zweite Zahl ein: ";

	cin>>zahlB;

	cout<<endl;


	if(zahlB<0)

	   zahlB=zahlB*(-1);


	for(i=0;i<zahlB;i++)

	    proAdd=proAdd+zahlA;




	cout<<"das ergebniss ist : "<<proAdd;

	cout<<endl;


}

nun ist es so das er z.b. bei

3*-5 rechnet aber das ergebnis halt net stimmt da ja 3*-5 = -15

sind

bzw was ist wenn 3 = -3 ist

wenn beide minus sind ist das egal da sich ja da die vorzeichen eh auflösen

??

Okay, nächster Denkanstoß:

  • Könnte man dann nicht in einer z.B. bool-Variablen speichern ob die Zahl negativ war?
  • Sagt die XOR irgendwas?

Falls nötig könnte ich Dir auch mal einen Codeschnipsel geben. :e@sy

EDIT:

bzw was ist wenn 3 = -3 ist

Öhm...da kann ich Dir grad nicht folgen.

also habe das problem jetzt so gelöst


#include<iostream>


using namespace std;


void main(void)

{ 

	int zahlA;

	int zahlB;

    int proAdd=0;

	int i;


	cout<<"Bitte geben sie Die Erste Zahl ein: ";

	cin>>zahlA;

	cout<<endl<<"Bitte geben sie die zweite Zahl ein: ";

	cin>>zahlB;

	cout<<endl;


	if(zahlB<0)

	   zahlB=zahlB*(-1),zahlA=zahlA*(-1);

    if(zahlA<0,zahlB<0)

		zahlA=zahlA*(-1);

	for(i=0;i<zahlB;i++)

	    proAdd=proAdd+zahlA;




	cout<<"das ergebniss ist : "<<proAdd;

	cout<<endl;


}

aber über deinen vorschlag wäre ich auch sehr erfreut

will ja was lernen

danke


#include<iostream>


using namespace std;


void main(void)

{ 

	int zahlA;

	int zahlB;

	int proAdd=0;

	bool ZahlANegativ = false;

	bool ZahlBNegativ = false;


	cout<<"Bitte geben sie Die Erste Zahl ein: ";

	cin>>zahlA;

	cout<<endl<<"Bitte geben sie die zweite Zahl ein: ";

	cin>>zahlB;

	cout<<endl;


	if (zahlA < 0) {

		ZahlANegativ = true;

		zahlA *= -1;

	}


	if (zahlB < 0) {

		ZahlBNegativ = true;

		zahlB *= -1;

	}


	for (int i = 0; i < zahlB; i++)

	    proAdd += zahlA;


	if (ZahlANegativ ^ ZahlBNegativ)

		proAdd *= -1;


	cout<<"das ergebniss ist : "<<proAdd;

	cout<<endl;

}

Büdde schön...

Alle Angaben wie immer ohne Gewähr, da nur im Gehirn entworfen... :D

Danke schön werde mir das mal genauer ansehen und auseinander flücken

muss nur jetzt die nächste aufgabe lösen naja da ist das anders rum

jetzt heist es: der ganzahligen quotienten a/b zweier zahlen

a und b und der entstehende rest soll über subtaktion berechnet werden

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.