0% found this document useful (0 votes)
17 views

Apdf

This program demonstrates swapping two numbers using call by value and call by reference methods. It prompts the user to select between the two methods and then prompts them to enter two numbers. For call by value, it passes the numbers directly to the swapvalue function. For call by reference, it passes the addresses of the numbers to the swapreference function. Both functions swap the numbers and print the results.

Uploaded by

Achin Pal
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Apdf

This program demonstrates swapping two numbers using call by value and call by reference methods. It prompts the user to select between the two methods and then prompts them to enter two numbers. For call by value, it passes the numbers directly to the swapvalue function. For call by reference, it passes the addresses of the numbers to the swapreference function. Both functions swap the numbers and print the results.

Uploaded by

Achin Pal
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Program no.

2
// Write a program to swap two nos. using call by
reference & call by value method.

#include<stdio.h>
#include<conio.h>
void swapvalue(int x,int y)
{
x=x+y;
y=x-y;
x=x-y;
printf(" \n After Swaping by Value \n ");
printf(" \n 1st number : %d \n 2nd number : %d ",x,y);
}
void swapreference(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void main()
{
int num1,num2,ch;
clrscr();
printf(" \n \n Swap Menu ");
printf(" \n \n 1. Call By Value ");
printf(" \n \n 2. Call By Reference ");
printf(" \n \n 3. Exit ");
printf(" \n \n Enter Your Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf(" \n Enter the 1st number : ");
scanf("%d",&num1);
printf(" \n Enter the 2nd number : ");
scanf("%d",&num2);
swapvalue(num1,num2);
break;
case 2:printf(" \n Enter the 1st number : ");
scanf("%d",&num1);
printf(" \n Enter the 2nd number : ");
scanf("%d",&num2);
swapreference(&num1,&num2);
printf(" \n After Swaping by Reference \n ");
printf(" \n 1st number : %d \n 2nd number : %d
",num1,num2);
break;
default:printf("\n Wrong Choice !!!!!!! ");
}
getch();}

You might also like