Hi folks, what’s happening? My last tutorial was about Calculate circumference, area and perimeter in C. Let’s look at how to swap two numbers in C Programming Language.This is the sixth program of [I] section of Chapter 1:Getting Started of the book, “Let us C” by “Yashwant Kanetkar”.Do let us know if you have any doubts or suggestions in the comments section below.


Two numbers are input through the keyboard in two locations C and D. Write a C program to interchange the contents of C and D.

Solution to swap two numbers in C

We accept two numbers from the user. We write a function swap with two parameters to allocate the memory locations. We then use a temporary location to store a number and then swap the numbers. We then, display the swapped numbers in the console. This program also displays the use of pointers.

Download the solution:

Download Program

Program to swap two numbers in C

void main()
int a,b;
printf("Enter two numbers separated by space: ");
scanf("%d%d",&a ,&b);
printf("\nBefore swapping: ");
printf("%d %d",a,b);
printf("\nAfter swapping: ");
printf("%d %d", a, b);
swap(int *x, int *y)
int t;
t = *x;
*x = *y;
*y = t;


Enter two numbers separated by a space: 4 2

Before swapping: 4 2

After swapping: 2 4

Download the solution:

Download Program
Read Also:

Introduction to C Programming

Calculate gross salary in C

Subtracting two numbers in C

Multiplying two numbers in C

Harsh Shah

Harsh Shah

A highly motivated individual with a hunger to learn and share as much as I can about everything. Masters in Interaction Design with a Bachelors in Computer Science. I have worked with clients across the globe on various design and tech projects. I believe in giving back and that's how ReadMeNow was founded.

Leave a Reply