C program swap two numbers : Implementation of this program is very simple.
Swapping of two numbers: For swapping numbers first we have 2 numbers and using the third variable we swap the numbers.

Example:
no1= 10 and no2=20
temp = no1 => temp=10
no1=no2 => no1=20
no2=temp => no2=10

Algorithm to swap two numbers using third variable

  1. Take two input values. no1 and no2
  2. Perform swapping using third variable
    temp= no1
    no1=no2
    no2=temp
  3. Print swapped values

C program swap two numbers

#include<stdio.h>
void main()
{
int first,second,temp; //declaring variable
printf("Enter first\n");
scanf("%d",&first); // take input first number
printf("Enter second \n");
scanf("%d",&second);//take second number input
printf("before swaping\n first = %d \n second = %d \n",first ,second );
temp = first ;
first = second;
second = temp;
printf("after swaping\n first = %d \n second =%d",first,second);
}

Output :

C program swap two numbers

Write A Comment