Swap two numbers without using third variable - Devbhoomi

FREE JOB ALERT & ONLINE TUTORIALS

Hot

Post Top Ad

Wednesday 8 November 2017

Swap two numbers without using third variable

#include <stdio.h>

int main()
{
    int x = 10, y = 5;

    printf("Enter x : ");
    scanf("%d", &x);
    printf("Enter y : ");
    scanf("%d", &y);

    // Code to swap x and y
    x = x + y;
    y = x - y;
    x = x - y;

    printf("\nAfter Swapping\nx = %d\ny = %d", x, y);

    return 0;
}
OUTPUT
Enter x : 15
Enter y : 5

After Swapping
x = 5
y = 15

No comments:

Post a Comment

Post Top Ad