C Program to Swap Two Numbers using Pointers

In this example, You learn about c program to swap two numbers using pointers.

Here you learn to swap two numbers using pointer, You are given two numbers and the task of the program to swap two numbers with pointers and functions.

C Program to Swap Two Numbers using Pointers

#include<stdio.h>
void swap(int *p, int *q);
void main()
{
  int *p,*q,a,b,temp;
  printf("Enter Two Numbers to be Swapped: ");
  scanf("%d %d",&a,&b);
  swap(&a,&b);
}
void swap(int *p,int *q)
{
  int temp;
  temp = *p;
  *p = *q;
  *q = temp;
  printf("Numbers After Swapping are: %d and %d",*p,*q);
}

Output
Enter Two Numbers to be Swapped: 12 -15
Numbers After Swapping are: -15 and 12
I hope you enjoy this example if you have doubt leave it in the comment box below.
Happy Coding 😊

If you have any doubts, Please let me know

Previous Post Next Post

Contact Form