Decode School

Decode School - The School for Problem Solvers

Welcome to my blog. Subscribe and get my latest blog post in your inbox.

C Program to Swap of two numbers without using third variable

Get two integers a and b from user and swap the values without using third variable

Sample Input 1:

6 5

Sample Output 1:

5 6

Program or Solution

					    
							
#include<stdio.h>

int main()

{
    
	int a,b,t;
	printf("Enter a:");
	scanf("%d",&a);
	printf("Enter b:");

	scanf("%d",&b);
	a=a+b;
	b=a-b;
	a=a-b;
	printf("\nAfter Swap\na: %d b: %d",a,b);  
	return 0;

}
					    
					

Program Explanation

Get a and b using scanf statement swap the values without using any extra variables.

Print a and b using printf statement