Saturday 11 November 2017

C Program to Swap two Integers without using Temporary Variables and Bitwise Operations


Code:

#include stdio.h

// Function Prototype
void swap(int *, int *);

void main()
{
    int x, y;
    printf("Enter 2 nos: \n");
    scanf("%d %d", &x, &y);
    printf("\nYou have entered x = %d y = %d \n", x, y);
    swap(&x,&y);    // passing the 2 nos to the swap function
}

// function to swap the two numbers
void swap(int *a, int *b)
{
    *a = *a + *b;
    *b = *a - *b;
    *a = *a - *b;
    printf("Swapped . . . .\n"); // printing the swapped numbers
    printf("x = %d y = %d\n", *a, *b);
}


Output:

Enter 2 nos:
4
7

You have entered x=4 y=7
Swapped . . . .
x=7 y=4

More C Programs:















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...