C Program To Swap Two Numbers

Write a c program to swap two numbers.

C program to swap two numbers

/* Aim: To swap two numbers */

#include<stdio.h>

int main()
{
	int a,b,t;
 
	printf("\n Enter First Number :");
	scanf("%d",&a);

	printf("\n Enter Second Number :");
	scanf("%d",&b);

	t=a;a=b;
	b=t;

	printf("\n After interchanging the numbers are as follows: \n");
	printf("\n First Number = %d ",a);
	printf("\n \n Second Number = %d \n \n ",t);

	return 0; 
}

/* Output of above code -

 Enter First Number :12

 Enter Second Number :21

 After interchanging the numbers are as follows: 

 First Number = 21 
 
 Second Number = 12 

*/

C program to swap two numbers using function


/* Aim: Write a function for the following function prototype
void Swap(int a,int */

#include<stdio.h>

void Swap(int a,int b); // Swap Function Prototype

int main()
{
	int a,b;
	printf("\n Enter Values of a and b:- ");
	scanf("%d%d",&a,&b);
	printf("\n Values before swapping a=%d and b=%d \n",a,b); 
	Swap(a,b);
    
    return 0;
}

// Swap Function
void Swap(int a,int b)
{
	int temp;

	temp=a;
	a=b;
	b=temp;
 
	printf("\n Values after swapping a=%d and b=%d \n \n",a,b);
  
}

Output

 Enter Values of a and b:- 4 2

 Values before swapping a=4 and b=2 

 Values after swapping a=2 and b=4