Using a temporary variable:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp;
clrscr():
printf("Enter the first number a\n");
scanf("%d",&a);
printf("Enter the second number b\n");
scanf("%d",&b);
printf("Before swapping a=%d b=%d",a,b);
temp=a;
a=b;
b=temp;
printf("The swapped values are: a=%d b=%d",a,b);
getch();
}
Without using a temporary variable:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the first number a\n");
scanf("%d",&a);
printf("Enter the second number b\n");
scanf("%d",&b);
printf("Before swapping a=%d b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("The swapped values are: a=%d b=%d\n");
getch();
}
Copyright © 2026 eLLeNow.com All Rights Reserved.