A C program to swap the values of two variables, using pointers – IGNOU MCA Assignment 2014 – 15

By | July 14, 2014

 MASTER OF COMPUTER APPLICATIONS
Course Code : MCS-011
Course Title : Problem Solving and Programming
Assignment Number : MCA(1)/011/Assign/2014-15
Maximum Marks : 100
Weightage : 25%

 

Write an interactive C program to swap the values of two given variables, using pointers.

 

# include <stdio.h>
void main()
{
int M,N;
void swapRef ( int *, int * );
clrscr();
printf(“ENTER M : “);
scanf(“%d”,&M);
printf(“ENTER N : “);
scanf(“%d”,&N);
printf (“\nBefore calling function swapRef M=%d N=%d”,M,N);
swapRef (&M,&N); /*address of arguments are passed */
printf(“\nAfter calling function swapRef M=%d N=%d”,M,N);
getch();
}
void swapRef (int *pm, int *pn)
{
int temp;
temp = *pm;
*pm = *pn;
*pn = temp;
printf (“\nWithin function swapRef *pm=%d *pn=%d”,*pm,*pn);
return;
}

Code:

[codesyntax lang=”c”]

# include <stdio.h>
void main()
{
int M,N;
void swapRef ( int *, int * );
clrscr();
printf("ENTER M : ");
scanf("%d",&M);
printf("ENTER N : ");
scanf("%d",&N);
printf ("\nBefore calling function swapRef M=%d N=%d",M,N);
swapRef (&M,&N); /*address of arguments are passed */
printf("\nAfter calling function swapRef M=%d N=%d",M,N);
getch();
}
void swapRef (int *pm, int *pn)
{
int temp;
temp = *pm;
*pm = *pn;
*pn = temp;
printf ("\nWithin function swapRef *pm=%d *pn=%d",*pm,*pn);
return;
}

[/codesyntax]

Screen Shots:

SWAP

 

SWAP_Output

 

Leave a Reply