Home / Programs / Write a c program to swap two numbers.
Programming Example

Write a c program to swap two numbers.

👁 1,202 Views
💻 Practical Program
📘 Step by Step Learning

In this program you will learn how to swap two numbers using c program.

Information & Algorithm

Here we used concept of function.

Here we used concept of pointer.

Here we used temp pointer variable.

Program Code

#include <stdio.h>
void swap(int *a, int *b)
{
	int temp;
	temp = *a;
	*a = *b;
	*b = temp;
}
int main()
{
	int x=5,y=10;
	void swap(int *,int *);
	printf("%d %d\n",x,y);
	swap(&x, &y);
	printf("%d %d\n",x,y);
	return 0;
}

Output

5 10
10 5

Explanation

None

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.