Source code:
/*Write a C program to swap two integers using pointers. You have to write a swap function that will accept the address of two integer and swap their values. */
#include <stdio.h>
void swap(int *a,int *b)
{
int t;
t = *a;
*a = *b;
*b = t;
}
int main()
{
int num1,num2;
printf("Enter 1st number: ");
scanf("%d",&num1); /*Enter value of num1 */
printf("Enter 2nd number: ");
scanf("%d",&num2); /*Enter value of num2 */
swap(&num1,&num2);
/*print values after swapping */
printf("num1=%d, num2=%d\n",num1,num2);
return 0;
}
Output:
No comments:
Post a Comment