C Programming:swapping with and without using third variable

Posted on:
tags: , ,


Q-4-a: Write a program to swap two numbers by using third variables.
#include<stdio.h>
#include<conio.h>
void main()
{
                int a,b,temp;
                clrscr();
                printf("\n\t\tENTER ANY NUMBER FOR a:");
                scanf("%d",&a);
                printf("\n\t\tENTER ANY NUMBER FOR b:");
                scanf("%d",&b);
                                temp=a;
                                a=b;
                                b=temp;
                printf("\n\t\tTHE SWAPPING OF a=%d AND b=%d",a,b);
                getch();
}


Q-4-a: Write a program to swap two numbers without using third variables.
#include<stdio.h>
#include<conio.h>
void main()
{
                int a,b;
                clrscr();
                printf("\n\t\tENTER ANY NUMBER FOR a:");
                scanf("%d",&a);
                printf("\n\t\tENTER ANY NUMBER FOR b:");
                scanf("%d",&b);
                                a=a+b;
                                b=a-b;
                                a=a-b;

                printf("\n\t\tTHE SWAPPING OF a=%d AND b=%d",a,b);
                getch();
}

No comments:

Post a Comment

< >