C program for swapping 2 numbers

Prev     Next

C program example for swapping 2 numbers:

What is swapping?

Swaping is nothing but interchanging. It may be the value of 2 numbers or something to exchange between them. For example, consider below example.

  • Before swapping, the values of A and B are as below.
    A = 10
    B = 20
  • After swapping, the values of A and B will be as below.
    A = 20
    B = 10

Example C program for swapping 2 numbers with temp variable:


Output of C program for swapping 2 numbers with temp variable:

Please enter the 1st number : 20
Please enter the 2nd number : 30
Before swapping:
A – 20
B – 30
After swapping:
A – 30
B – 20

Example C program for swapping 2 numbers without temp variable:

Output of C program for swapping 2 numbers without temp variable:

Please enter the 1st number : 45
Please enter the 2nd number : 67
Before swapping:
A – 45
B – 67
After swapping:
A – 67
B – 45

Prev     Next






Share this website !!!
Facebook Twitter Linkedin