Basic

Switch Case

Goto

Operators

if Statement

Nested if

While Loop

For Loop

Patterns

Array

2D Array

String Function Example

Pointers

Recursion Function

Structure

Excersises

Others


Swapping Two Numbers Without 3rd Variable Using C Program


Swapping means exchanging the values of two Variables.The following example shows how to Swap two numbers without third variable using C Program.

Example : pgm.c

#include<stdio.h>
int main(){
    int a,b,c;
    printf("\nEnter the value of A and B : ");
    scanf("%d%d",&a,&b);
    printf("\nBefore Swapping : ");
    printf("\nA value is %d",a);
    printf("\nB value is %d",b);
    a=a+b;
    b=a-b;
    a=a-b;
    printf("\nAfter Swapping : ");
    printf("\nA value is %d",a);
    printf("\nB value is %d",b);
    return 0;
}

Output:

Enter the value of A and B :
 10
 20

Before Swapping :
 A value is 10
 B value is 20

After Swapping :
 A value is 20
 B value is 10