Sunday, 13 March 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




Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment