Computer Science, asked by jhkaran6065, 6 months ago

Write a program to interchange the value of two numbers
using the third value.​

Answers

Answered by Anonymous
1

Explanation:

Swapping Two Numbers Using Third Variable

Assign var1 value to a temp variable: temp = var1.

Assign var2 value to var1: var1 = var2.

Assign temp value to var2: var2 = temp

hope it's helpful to you.

Answered by BrainlyPink
0

Swapping two number in C programming language means exchanging the values of two variables. Suppose you have two variable var1 & var2. Value of var1 is 20 & value of var2 is 40. So, after swapping the value of var1 will become 40 & value of var 2 will become 20. In this blog will understand how to swap two variables in C.

Swapping Two Numbers Using Third Variable

Swapping Two Numbers Using Without Using Third Variable

Swapping Function in C

Swap two numbers using pointers in C

Swap Two Numbers Using Bitwise XOR

We will look at each one of them one by one.

Swapping Two Numbers Using Third Variable

Logic

The idea behind swapping two numbers using 3rd variable is simple. Store the value of 1st variable in temporary variable. Store the value of 2nd variable in the first variable. At last, store the value of temp variable in 2nd variable. In this program, we are using a temporary variable to hold the value of the first variable.

Assign var1 value to a temp variable: temp = var1

Assign var2 value to var1: var1 = var2

Assign temp value to var2: var2 = temp

Code:

1

2

3

temp = var1;

var1 = var2;

var2 = temp;

Now, let’s look at the complete code.

Example

1

2

3

4

5

6

7

8

9

10

11

12

13

#include <stdio.h>  

int main()

{

int var1, var2, temp;  

printf("Enter two integersn");

scanf("%d%d", &var1, &var2);

printf("Before SwappingnFirst variable = %dnSecond variable = %dn", var1, var2);

temp = var1;

var1 = var2;

var2 = temp;

printf("After SwappingnFirst variable = %dnSecond variable = %dn", var1, var2);

return 0;

}

Similar questions