C++ Examples

  1. Write a C++ Program to print a welcome text in a separate line.

Swapping of two numbers in C++ without using third variable.

#include <iostream>
using namespace std;

int main()
{   
    //Variable declaration                     
    
    int number1,number2;            
    cout<<"Enter your first number to swap ";
    cin>>number1;
    cout<<"Enter your second number to swap ";
    cin>>number2;
    
    cout<<"Before swaping the numbers are "<<number1<<" and "<<number2<<endl;
    // swaping of Variable using airthmetic operators
    number1 = number1 + number2;
    number2 = number1 - number2;
    number1 = number1 - number2;
    
    cout<<"After swaping the numbers are "<<number1<<" and "<<number2<<endl;
    
    return 0;
    
}

Output

Enter your first number to swap 4
Enter your second number to swap 6
Before swaping the numbers are 4 and 6
After swaping the numbers are 6 and 4
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments