Friday 10 June 2016

Swap 2 numbers

This can be done without using a temporary variable.Here's the idea
no1 = 3 , no2 = 5
no1 = 8
store 8-5 = 3 in no2
store 8-3 = 5 in no1


Code

#include<stdio.h>

int main()
{
    int no1,no2;

    printf(" Enter the 2 nos ");
    scanf("%d %d",&no1,&no2);

    printf("\n Before swapping no1 = %d , no2 = %d",no1,no2);

    no1 = no1 + no2;

    no2 = no1 - no2;
    no1 = no1 - no2;

    printf("\n After swapping  no1 = %d , no2 = %d",no1,no2);

    return 0;
}


O/P





No comments:

Post a Comment