Sunday, 15 November 2015

C++ Program To Swap Two Numbers Using Functions

Problem :- Write A C++ Program To Swap Two Numbers Call By Reference/Call By Value Using Functions .

Logic :- There are two method you can use to swap Two Number using function 
1. Call by Reference
2. Call by Value

Call by Reference :- In Call by reference we pass address of variable in function not value of variable .

Call by Value :- In Call by Value we pass value of variable in function .

Solution :-

Method 1 :- Call by Reference

#include<iostream>
using namespace std;

void swap(int *x ,int *y );
//Call By Reference
int main()
{
   //By-Ghanendra Yadav
    int a,b;
    cout<<"\nEnter Two Number You Want To Swap \n";
    cin>>a>>b;
    
    swap(&a,&b);
    
cout<<"\nAfter Swapping Numbers Are Given below\n\n";
    cout<<a<<"   "<<b<<" \n";
    
    return 0;
}
void swap(int *x,int *y)
{
int z;
z=*x;
*x=*y;
*y=z;
}

Method 2 :- Call by Value

#include<iostream>
using namespace std;

void swap(int ,int );
//Call By Value
int main()
{
   //By-Ghanendra Yadav
    int a,b;
    cout<<"\nEnter Two Number You Want To Swap \n";
    cin>>a>>b;
    cout<<"\nAfter Swapping Numbers Are Given below\n\n";
    swap(a,b);
    return 0;
}
void swap(int x,int y)
{
int z;
z=x;
x=y;
y=z;
cout<<x<<"   "<<y<<" \n";
}

Output:-

1. Call By Reference
C++ Program To Swap Two Numbers Using Functions

2. Call By Value
Write A C++ Program To Swap Two Numbers Call By Reference/Call By Value Using Functions


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

6 comments:

  1. Thank you so much for sharing this worth able content with us. The concept taken here will be useful for my future programs and i will surely implement them in my study. Keep blogging article like this.

    Java Training in Bangalore|

    ReplyDelete
  2. I and my friends were going through the nice, helpful tips from the blog then the sudden came up with an awful suspicion I never expressed respect to the website owner for those secrets."Devops Training in Bangalore"

    ReplyDelete
  3. It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read.
    I’ve bookmarked your site, and I’m adding your RSS feeds to my Google account.
    java training in bangalore

    ReplyDelete
  4. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete