Using Call by Reference Swap Numbers

By | December 9, 2022

Using Call by Reference Swap Numbers – Write a C program to swap two values by call by reference method of parameter passing.

Using Call by Reference Swap Numbers

Program for Passing Parameters – Call By Reference

#include <stdio.h>
void swap2numbers(int *n1, int *n2);
int main()
{
    int num1,num2;
    printf("\n\n Using C Pointers : Swap Numbers Using Call by Reference :\n"); 
    printf("--------------------------------------------------------------\n");	
    printf(" Enter First Number : ");
    scanf("%d",&num1);
    printf(" Enter Second Number : ");
    scanf("%d",&num2);
	
	
    printf("\n The values before swapping are :\n");
    printf(" Number 1 = %d\n Number 2 = %d\n",num1,num2);
    swap2numbers(&num1, &num2);
    printf("\n The values after swapping are :\n");
    printf(" Number 1 = %d\n Number 2 = %d\n",num1,num2);
    return 0;
}
void swap2numbers(int *n1, int *n2)
{
    int temp;
    temp=*n1;
    *n1=*n2;
    *n2=temp;
}

Output

Using C Pointers : Swap Numbers Using Call by Reference :
————————————————————–
Enter First Number : 105
Enter Second Number : 600

The values before swapping are :
Number 1 = 105
Number 2 = 600

The values after swapping are :
Number 1 = 600
Number 2 = 105

You will also like to learn more:

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *