13/03/2016

C++ Program For Swapping Number Using Pointer

Problem:- C++ program to swap two numbers using pointers or Swapping two variable using pointers or C++ program to swap two numbers using call by value or C Program to Swap Two Numbers / Variables using Pointer or Simple Example Program for Swap Numbers Using Pointers In C++ or swap 2 numbers using pointers in C or C++ Program to Swap Two Numbers or C++ function call by pointer or C Program to Swapping Two Numbers Using a Temporary Variable or C Program to Swapping Two Numbers Using Bitwise Operators or C++ pointers declaring, passing pointer to function or C program to swap two numbers or C Program to Swap Two Numbers Using Pointer or program to swap two numbers using pointers and functions or write a code to swap two numbers using reference in c++.

Check This:- Hacker rank solution for Strings, Classes, STL, Inheritance in C++.


Explanation:- Swapping the two number is very easy we have to take a two number entered by user and pass the both two number in function by reference or pass by value here we are passing the both number by reference(passing address of variable) and in function we put the swapping condition in function as you can see in the below program. after the swapping the number either we can return the result to function or we can directly print the swapped numbers in function. In this swapping method (call by reference) anything changed in the value will also affect the actual parameters but here we print the swapped value in the same function But it is not necessary we can directly print the value in the main function cause we pass the value through call by reference not call by value.

Also Check:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.

Extreme Recommended:- If this post is beneficial for you and you want Updates for New post then please like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date for a new post or if you have any Query you can ask there with lots of coders also suggest to your Friends to join and like our page, so we can help our community, and don't forget to Subscribe. Enter your Email and click to subscribe.



Solution:-

#include<bits/stdc++.h>
using namespace std;

void swap(int&, int&);    

int main()
{
 /*visit - programmingwithbasics.com*/
    int first, second;

cout<<"Enter The Two Value \n";
    cin>>first>>second;
    
    cout << "\nBefore Swapping Values Are\n" << endl;
cout<<"First Value is = "<<first<<endl;
cout<<"Second Value is = "<<second<<endl;
    swap(first, second);
return 0;
}

void swap(int &first, int &second)
{
    int temp;
    temp = first;
    first = second;
    second = temp;
    
    cout << "\n\nAfter Swapping Values Are\n" << endl;
cout<<"First Value is = "<<first<<endl;
cout<<"Second Value is = "<<second<<endl;
}


Output:-

c++ program to swap two numbers using return by reference



You May Also Like


1. C++ Program To Check Character Is Uppercase, Lowercase Alphabet Or A Digit Or A Special Symbol

2. C++ Program To Find Greatest Among Three Numbers

3. C++ Program To Check Number Is Armstrong Or Not Using If/Else Statements

4. C++ Program To Find Character Is Vowel Or Not Using If/Else Statements

5. C++ Program To Check Number Is Even Or Odd Using If/Else Statements

6. C++ Program To Check Number Is Prime Or Not Using If/Else Statements




Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: