Einführung in das Tauschen in C ++
Tauschen ist nichts anderes als ein Datenaustausch zwischen Variablen. Wie jede andere Sprache können wir auch in C ++ Swap-Operationen ausführen. Es wird mit zwei Methoden ausgeführt - mit der dritten Variablen und ohne die dritte Variable. In diesem Artikel werden diese beiden Methoden zum Vertauschen von Zahlen anhand von Beispielen erläutert. Um das Swap-Konzept zu verstehen, gehen wir auf ein Beispiel ein: Angenommen, Sie haben 500 Banknoten und benötigen einen Tausch von 500 Rupien. Sie haben Ihren Freund um die 500 gebeten und er gibt Ihnen 5 Banknoten von 100 als Gegenleistung 500 Banknoten. In diesem Fall tauschen Sie und Ihr Freund nur die Noten aus. Dies wird als Datenaustausch zwischen zwei Variablen bezeichnet.
Wie funktioniert das Austauschen in der C ++ - Sprache?
Tauschen heißt Daten austauschen. In C ++ kann das Austauschen mit zwei Methoden durchgeführt werden. Erstens wird die dritte Variable verwendet, dh die temporäre Variable, und zweitens wird die dritte Variable nicht verwendet. In diesem Abschnitt erfahren Sie, wie Sie mit beiden Methoden zwei und drei Zahlen vertauschen.
Beispiel 1
Zwei Zahlen tauschen Mit der dritten Variablen.
Programm
#include
using namespace std;
int main()
(
int first_num, second_num, temp_num;
cout << "Enter first number: "; //allow user to add first number
cin >> first_num;
cout << "Enter second number: "; //allow user to add second number
cin >> second_num;
cout << "Before swapping " << endl;
cout << "First number: "<< first_num << endl;
cout << "Second number: " << second_num < temp_num = first_num; //first number is assigned to temp
first_num = second_num; //second number is assigned to first number
second_num = temp_num; //first number is assigned to secind number
cout << "After swapping" << endl;
cout << "First number: " << first_num << endl;
cout << "Second number: " << second_num;
return 0;
)#include
using namespace std;
int main()
(
int first_num, second_num, temp_num;
cout << "Enter first number: "; //allow user to add first number
cin >> first_num;
cout << "Enter second number: "; //allow user to add second number
cin >> second_num;
cout << "Before swapping " << endl;
cout << "First number: "<< first_num << endl;
cout << "Second number: " << second_num < temp_num = first_num; //first number is assigned to temp
first_num = second_num; //second number is assigned to first number
second_num = temp_num; //first number is assigned to secind number
cout << "After swapping" << endl;
cout << "First number: " << first_num << endl;
cout << "Second number: " << second_num;
return 0;
)
Ausgabe:
Beispiel # 2
Tauschen Sie zwei Zahlen aus, ohne die dritte Variable zu verwenden.
Programm
#include
using namespace std;
int main()
(
int first_num, second_num;
cout << "Enter first number: ";
cin >> first_num; //9
cout << "Enter second number: ";
cin >> second_num; //10
cout << "Before swapping " << endl;
cout << "First number: "<< first_num << endl;
cout << "Second number: " << second_num << endl;
first_num = first_num * second_num; //9 * 10 = 90
second_num = first_num / second_num; // 90 / 10 = 9
first_num = first_num / second_num; // 90 / 9= 10
cout << "After swapping" << endl;
cout << "First number: " << first_num << endl; 10
cout << "Second number: " << second_num << endl; //9
return 0;
)
Ausgabe:
Beispiel # 3
Drei Zahlen in C ++ tauschen Mit der dritten Variablen.
Programm
#include
using namespace std;
int main()
(
int first_num, second_num, third_num, temp_num;
cout << "Enter first number: "; //allow user to add first number
cin >> first_num;
cout << "Enter second number: "; //allow user to add second number
cin >> second_num;
cout << "Enter third number: "; //allow user to add third number
cin >> third_num;
cout << "Before swapping" << endl;
cout << "First number: "<< first_num << endl;
cout << "Second number: " << second_num << endl;
cout << "Third number: "<< third_num << endl;
temp_num =first_num;
first_num = second_num; //second number is assigned to first number
second_num = third_num; //third number is assigned to second number
third_num = temp_num; //first number is assigned to third number
cout << "After swapping" << endl;
cout << "First number: " << first_num << endl;
cout << "Second number: " << second_num << endl;
cout << "Third number: " << third_num << endl;
return 0;
)
Ausgabe:
Beispiel # 4
Tauschen Sie drei Zahlen aus, ohne die dritte Variable zu verwenden.
Programm
#include
using namespace std;
int main()
(
int first_num, second_num, third_num;
cout << "Enter first number: ";
cin >> first_num; //10
cout << "Enter second number: ";
cin >> second_num; //5
cout << "Enter third number: ";
cin >> third_num; //20
cout << "Before swapping" << endl;
cout << "First number: "<< first_num << endl;
cout << "Second number: " << second_num << endl;
cout << "Third number: " << third_num << endl;
first_num = first_num + second_num + third_num; // 10 + 5 + 20= 35
second_num = first_num - (second_num + third_num); // 35 - (5 + 20) = 10
third_num = first_num - (second_num + third_num); // 35 - (10 + 20) = 5
first_num = first_num - (second_num + third_num); 35 - (10 + 5) = 20
cout << "After swapping" << endl;
cout << "First number: " << first_num << endl; //20
cout << "Second number: "<< second_num << endl; //10
cout << "Third number: " << third_num << endl; //5
return 0;
)
Ausgabe:
Fazit
In diesem Artikel haben wir gesehen, wie zwei und drei Zahlen in C ++ mit der dritten Variablen und ohne die dritte Variable vertauscht werden. Ich hoffe, Sie finden diesen Artikel hilfreich.
Empfohlene Artikel
Dies ist eine Anleitung zum Tauschen in Python. Hier besprechen wir, wie das Austauschen in der C ++ - Sprache mit Beispielen und Ausgaben funktioniert. Sie können auch den folgenden Artikel lesen, um mehr zu erfahren -
- Überladen in C ++
- Quadratwurzel in C ++
- C ++ Alternativen
- Sternchenmuster In c ++
- In PHP tauschen
- Überladen in Java
- Python-Überladung
- Quadratwurzel in PHP
- Top 11 Features und Vorteile von C ++
- Quadratwurzel in JavaScript