In: Computer Science
Write a C++ program to swap two numbers and show your output
#include <iostream>
using namespace std;
int main()
{
int number1, number2;
cout << "Enter the value for number1 : " << endl;
cin >> number1; //taking first number
cout << "Enter the value for number2 : " << endl;
cin >> number2; //taking second number
cout << "You Entered number1 as: " << number1 << " and number2 as: " << number2 << endl;
int number3;
number3 = number1; //Swapping two numbers using third variable i.e number3
number1 = number2;
number2 = number3;
cout<<"After Swapping, number1 is : "<<number1<<" number2 is : "<<number2;
return 0;
}
output :
Enter the value for number1 :
10
Enter the value for number2 :
20
You Entered number1 as: 10 and number2 as: 20
After Swapping, number1 is : 20 number2 is : 10
proof of execution :
If you need any changes, please comment.