C program to swap two numbers without using third variable

devquora
devquora

Posted On: Dec 23, 2020

C program to swap two numbers without using third variable

 

Overview:

In this article First of all, I want to introduce you with Numbers. Then I will explain What is Swapping? Furthermore, I will demonstrate and write logic for the swaping of two numbers without third variable. After all, I will show a C Program to swap two numbers.

Table of contents:

  1. What is a Number?
  2. What is swapping?
  3. Demonstration of Swapping two numbers
  4. How to swap values of two variables without third variable?
  5. C program to swap two integers
  6. Conclusion

What is a Number?

Number is an object that uses digits to perform mathematical task. Calculus that is a branch of mathematics included many numbers such as integer, whole number, real number, and imaginary numbers so on. Here, I will discuss about number to perform arithmetic addition operation. Moreover, Number is a combination of digits, some symbols and decimal point. For instance, 127.23 is a rational number.

What is swapping?

Swapping is a mechanism by which you can exchange the value of two variables. For instance, If First number has value 13 and Second number has value 26 Then, swapping stores 26 to First number and 13 to Second number.

How to swap values of two variables without third variable?

  • Step 1: Take two variables as num1 and num2 and store some value to it.
  • Step 2: Then, Assign num1+num2 to a num1.
  • Step 3: Assign num1-num2 to num2.
  • Step 4: Assign num1-num2 to num1. After all, display num1 and num2.

Demonstration of Swapping two numbers

Demo of Swapping

C program to swap two numbers without third variable

#include
int main ()
{
int a=10, b=20;
printf ("Before swap a=%d b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf ("\nAfter swap a=%d b=%d",a,b);
return 0;
}

The output of the Program:

Before swap a=10 b=20

After swap a=20 b=10

swap without third variable

Conclusion:

This Program stores two integers one by one into the int datatype. After the successful swapping of numbers it will exchange the value of them at once or you can say that it assign a's value to b and b's value to a.

    Related Questions

    Please Login or Register to leave a response.

    Related Articles

    C Program Examples

    C Program to convert Decimal to Binary Number

    A Decimal Number is constructed with any digit from 0 to 9. For instance, 24 is a Decimal Number constructed from the digits 2 and 4. A Binary Number is constructed with digits 0 and 1. For instance, ..

    C Program Examples

    C program to reverse a string

    This program reversed the entered string that means opposite of the previous string sequence...

    C Program Examples

    C program to reverse an Integer number

    Are you want a demonstration of The C Program that reverses an Integer number? This Program reversing the sequence of digits in an Integer. After the successful compilation of the program, a message..