Bookmark and Share

free counters
Back to Problem list

Problem 13: Swaping the value of two variables.

Solution: Very easy one! Swap means interchange between two variables.

Let, int a = 5, b = 10, after swaping, we get, a = 10 and b = 5;

1st solution, using temp variable,
int c;
c = a;
a = b;
b = c;

2nd Solution, without using temp variable...

b = a + b;
a = b - a;
b = b - a;

Complete Code:

#include<stdio.h>

int main() {
	int a, b;
	scanf("%d%d",&a,&b);
	b = a + b;
	a = b - a;
	b = b - a;
	printf("%d %d\n",a,b);
	return 0;
}


Back to Problem list