In this source code example, we will a C program to swap two numbers using a third variable.
C Program to Swap Two Numbers using Third Variable
In this C program, we will take input from the User or console and print the result to the output:
#include<stdio.h>
main() {
/*Declare the variables*/
int num1, num2, temp;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
printf("Numbers before swapping: %d %d ", num1, num2);
/*swapping the values of variables*/
temp = num1;
num1 = num2;
num2 = temp;
printf(" \nNumbers after swapping: %d %d ", num1, num2);
}
Output:
Enter first number: 10
Enter second number: 20
Numbers before swapping: 10 20
Numbers after swapping: 20 10