Friday, 4 December 2015

SWAPPING TWO NUMBERS WITHOUT USING TEMPORARY VARIABLE

AIM
To write a C program for swapping two numbers without using temporary variable
ALGORITHM
Step 1: Start
Step 2: Read two numbers a,b
Step 3: a←a+b
Step 4: b←a-b
Step 5: a←a-b
Step 6:Print the numbers a,b
Step 7:Stop

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    printf("Before swapping\na=%d\nb=%d\n",a,b);
    a=a+b;
    b=a-b;
    a=a-b;
    printf("\nAfter swapping\na=%\nb =%d ",a,b);
    getch();
}
RESULT

The C program for swapping two numbers is completed successfully and the output is verified.




No comments:

Post a Comment