C++ program to swap two values by Call by Value || call by value || Pointers || C++
In this, we are going to see how to the values of two variables by using Call By Value in C++ Programming Language.

The Code given below can be used for TURBO C++ Compilers: -

//Program for swapping(call by reference) the values of 2 number using pointer

#include <iostream.h>
#include <conio.h>

float swap(float *a, float *b)
{
    float *pt = a;
    a = b;
    b = pt;
}

void main()
{
    clrscr();
    float x, y, a, b;
    cout << "Enter 2 nubers:-\n";
    cin >> x >> y;

    cout << "Before Swapping:-\n";
    cout << "x = " << x << "\ny= " << y << endl;

    swap(&x, &y);

    cout << "After Swapping:-\n";
    cout << "x = " << x << "\ny= " << y << endl;
    getch();
}

//...........Coded by YASH ALAPURIA
  

The Code given below can be used for gcc/g++ Compilers: -

//Program for swapping(call by reference) the values of 2 number using pointer

#include <iostream>
#include <stdlib.h>
using namespace std;

float swap(float *a, float *b)
{
    float *pt = a;
    a = b;
    b = pt;
}

int main()
{
    system("cls");
    float x, y, a, b;
    cout << "Enter 2 nubers:-\n";
    cin >> x >> y;

    cout << "Before Swapping:-\n";
    cout << "x = " << x << "\ny= " << y << endl;

    swap(&x, &y);

    cout << "After Swapping:-\n";
    cout << "x = " << x << "\ny= " << y << endl;
    return 0;
}

//...........Coded by YASH ALAPURIA
  
#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post