Intercambio en C ++ - ¿Cómo funciona el intercambio en el lenguaje C ++?

Tabla de contenido:

Anonim

Introducción sobre el intercambio en C ++

El intercambio no es más que un intercambio de datos entre variables. Al igual que cualquier otro lenguaje, también podemos realizar operaciones de intercambio en C ++. Se realiza usando dos métodos: usando la tercera variable y sin usar la tercera variable. En este artículo, vamos a discutir estos dos métodos para intercambiar números con la ayuda de ejemplos. Para comprender el concepto de intercambio, analicemos un ejemplo: suponga que tiene 500 notas y necesita un intercambio de 500 rupias. Le pediste a tu amigo el intercambio de 500 y él te da 5 notas de 100 a cambio de 500 notas. Aquí, en este caso, usted y su amigo simplemente intercambian las notas. Esto es lo que se llama intercambio de datos entre dos variables.

¿Cómo funciona el intercambio en el lenguaje C ++?

Intercambiar significa intercambiar datos. En C ++, el intercambio se puede hacer usando dos métodos. La primera es intercambiar usando la tercera variable, es decir, la variable temporal y la segunda es sin usar la tercera variable. En esta sección, veremos cómo intercambiar dos y tres números usando ambos métodos.

Ejemplo 1

Intercambiando dos números usando la tercera variable.

Programa

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

Salida:

Ejemplo # 2

Intercambiando dos números sin usar la tercera variable.

Programa

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

Salida:

Ejemplo # 3

Intercambiando tres números en C ++ usando la tercera variable.

Programa

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

Salida:

Ejemplo # 4

Intercambiando tres números sin usar la tercera variable.

Programa

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

Salida:

Conclusión

En este artículo, hemos visto cómo intercambiar dos y tres números en C ++ usando la tercera variable y sin usar la tercera variable. Espero que encuentres útil este artículo.

Artículos recomendados

Esta es una guía para el intercambio en Python. Aquí discutimos cómo funciona el intercambio en el lenguaje C ++ con ejemplos y salidas. También puede consultar el siguiente artículo para obtener más información:

  1. Sobrecarga en C ++
  2. Raíz cuadrada en C ++
  3. Alternativas de C ++
  4. Patrones de estrellas en c ++
  5. Intercambio en PHP
  6. Sobrecarga en Java
  7. Sobrecarga de Python
  8. Raíz cuadrada en PHP
  9. Las 11 características y ventajas principales de C ++
  10. Raíz cuadrada en JavaScript