Write the program to swap two numbers (Exchange the value between two numbers)

 

Simple Program Example

Write the program to swap two numbers (exchange the value between two numbers).

swap.c (Name of program)

I.                  Code of Program:

Write the program to swap two numbers using third variable (exchange the value between two numbers using third variable).

1.     /* this program display the addition of two number on console.

2.     Written by www.abccode.in

3.     stdio.h provide standard input output functions.

4.     conio.h provide console input output functions.

5.     Check out our ABCCode YouTube channel to watch a video of

6.     this program.*/

7.       #include<stdio.h>

8.       #include<conio.h>

9.       void main()

10.    {

11.                                  int a, b, c;  // a, b and c are the integers declared here.

12.                                  printf("Enter the integer value for a and b \n");

13.                                  scanf("%d %d ",&a, &b);

14.                                  printf("VALUES BEFOR SWAP a = %d b = %d \n",a,b);

15.                                  c = a; // Store the value of a into variable c.

16.                                  a = b; // Exchange the value of b in variable a.

17.                                  b = c; // Exchange the value of a in variable b.

18.                                  printf("VALUES AFTER SWAP a = %d b = %d \n",a,b);

19.                                  getch();

20.     }

Explanation of Program Code:

Line 1 to 6: First six lines are comment lines.

Line 7: Library file stdio.h provide standard input output functions.

Line 8: Library file conio.h provide console input output functions.

Line 9: main function heading line. Start the execution of program from this line. 

Line 10: “{“ this indicate the beginning of main function.

Line 11: “a”, “b” and “c” are variable names that are used to store integer number. We must use integer data type to store the number value without floating point. int used to declare the integer variable.

Variable must be declared before use in program code. In the later chapter we will discuss the data type in detail. Variable declared without value. Swap or exchange the value of variable “a” and “b”. Variable “c” used in exchange operation.

Line 12: Display the message to user for enter two integer values. For that purpose used Standard output function printf.

Line 13: Accept the two integer values. For that purpose used Standard input function scanf.

Line 14: Display the values of accepted variables before the swap or exchange.

Line 15: Store the value of a into variable c. Use expression c = a.

Line 16: Swap or exchange the value of b into variable a. Use expression a = b.

Line 17: Swap or exchange the value of c into variable b. Use expression b = c. (Indirectly swap or exchange value of a into b.)

Line 18: Display the values of variables after the swap or exchange.

Line 19: The getch function is the console output function in the conio.h library file. That pause the output console until a key is pressed.  

Line 20: “}“ this indicate the end of main function.

Output of Program:

Enter the integer value for a and b                                                         

10 20                                                                                                      

VALUES BEFOR SWAP a = 10 b = 20                                              

VALUES AFTER SWAP a = 20 b = 10   

Use code and try to run:

/* this program display the addition of two number on console.

Written by www.abccode.in

stdio.h provide standard input output functions.

conio.h provide console input output functions.

Check out our ABCCode YouTube channel to watch a video of this program.*/

 #include<stdio.h>

 #include<conio.h>

 void main()

 {

          int a, b, c;  // a, b and c are the integers declared here.

          printf("Enter the integer value for a and b \n");

          scanf("%d%d",&a,&b);

          printf("VALUES BEFOR SWAP a = %d b = %d \n",a,b);

          c = a; // Store the value of a into variable c.

          a = b; // Exchange the value of b in variable a.

          b = c; // Exchange the value of a in variable b.

          printf("VALUES AFTER SWAP a = %d b = %d \n",a,b);

          getch();

}

II.              Code of Program:

Write the program to swap two numbers without using third variable (exchange the value between two numbers without using third variable).

1.                 /* this program display the addition of two number on console.

2.                 Written by  www.abccode.in

3.                 stdio.h provide standard input output functions.

4.                 conio.h provide console input output functions.

5.                 Check out our ABCCode YouTube channel to watch a video

6.                 of this program.*/

7.                 #include<stdio.h>

8.                 #include<conio.h>

9.                 void main()

10.            {

11.                      int a, b;  // a and b are the two integers declared here.

12.                      printf("Enter the integer value for a and b \n");

13.                      scanf("%d%d",&a, &b);

14.                      printf("VALUES BEFOR SWAP a = %d b = %d \n",a,b);

15.                      a = a + b; // Store the addition in variable a.

16.                      b = a - b; // Exchange the value of a in variable b.

17.                      a = a - b; // Exchange the value of b in variable a.

18.                      printf("VALUES AFTER SWAP a = %d b = %d \n",a,b);

19.                      getch();

20.            }

Explanation of Program Code:

Swap or exchange the two integer variable without using third variable. Simply use the following logic.

Store the addition of both variable in one variable. (a = a + b ;)

Exchange the value of a in variable b. (b = a - b ;)

Exchange the value of b in variable a. (a = a - b ;)

Output of Program:

Enter the integer value for a and b                                                         

10 20                                                                                                      

VALUES BEFOR SWAP a = 10 b = 20                                              

VALUES AFTER SWAP a = 20 b = 10      

Use code and try to run:

/* this program display the addition of two number on console.

Written by  www.abccode.in

stdio.h provide standard input output functions.

conio.h provide console input output functions.

Check out our ABCCode YouTube channel to watch a video

of this program.*/

#include<stdio.h>

#include<conio.h>

void main()

{

                   int a, b;  // a and b are the two integers declared here.

                   printf("Enter the integer value for a and b \n");

                   scanf("%d%d",&a, &b);

                   printf("VALUES BEFOR SWAP a = %d b = %d \n",a,b);

                   a = a + b; // Exchange the value of b in variable a.

                   b = a - b; // Exchange the value of b in variable a.

                   a = a - b; // Exchange the value of b in variable a.

                   printf("VALUES AFTER SWAP a = %d b = %d \n",a,b);

                   getch();

}

Comments