Échange en C ++ - Comment fonctionne l'échange dans le langage C ++?

Table des matières:

Anonim

Introduction sur l'échange en C ++

L'échange n'est rien d'autre qu'un échange de données entre variables. Comme tout autre langage, nous pouvons également effectuer des opérations de permutation en C ++. Elle est effectuée en utilisant deux méthodes - en utilisant la troisième variable et sans utiliser la troisième variable. Dans cet article, nous allons discuter de ces deux méthodes pour échanger des numéros à l'aide d'exemples. Pour comprendre le concept d'échange, discutons d'un exemple - supposons que vous ayez 500 notes et que vous ayez besoin d'un échange de 500 roupies. Vous avez demandé à votre ami l'échange de 500 et il vous donne 5 notes de 100 en retour de 500 notes. Ici, dans ce cas, vous et votre ami échangez simplement les notes. C'est ce qu'on a appelé un échange de données entre deux variables.

Comment fonctionne l'échange dans le langage C ++?

L'échange signifie l'échange de données. En C ++, l'échange peut être effectué à l'aide de deux méthodes. La première consiste à permuter en utilisant la troisième variable, c'est-à-dire la variable temporaire et la seconde, sans utiliser la troisième variable. Dans cette section, nous allons voir comment échanger deux et trois nombres en utilisant les deux méthodes.

Exemple 1

Échange de deux nombres à l'aide de la troisième variable.

Programme

#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;
)

Production:

Exemple # 2

Échange de deux nombres sans utiliser la troisième variable.

Programme

#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;
)

Production:

Exemple # 3

Échange de trois nombres en C ++ à l'aide de la troisième variable.

Programme

#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;
)

Production:

Exemple # 4

Échange de trois nombres sans utiliser la troisième variable.

Programme

#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;
)

Production:

Conclusion

Dans cet article, nous avons vu comment échanger deux et trois nombres en C ++ à l'aide de la troisième variable et sans utiliser la troisième variable. J'espère que cet article vous sera utile.

Articles recommandés

Ceci est un guide pour échanger en Python. Nous discutons ici du fonctionnement du swapping dans le langage C ++ avec des exemples et des sorties. Vous pouvez également consulter l'article suivant pour en savoir plus -

  1. Surcharge en C ++
  2. Racine carrée en C ++
  3. Alternatives C ++
  4. Motifs d'étoiles en c ++
  5. Échange en PHP
  6. Surcharge en Java
  7. Surcharge Python
  8. Racine carrée en PHP
  9. Les 11 principales fonctionnalités et avantages de C ++
  10. Racine carrée en JavaScript