A Program to Swap two Numbers
In this example, we are giving a program to swap two numbers which are given by the user.
Program to swap two numbers:
#include<stdio.h>
int main() {
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
// Value of first number assigned into temp
temp = first;
// Value of second is assigned to first
first = second;
// Value of temp is assigned to second
second = temp;
printf("\nAfter swapping, first number = %.2lf\n", first);
printf("After swapping, second number = %.2lf", second);
return 0;
}
Output:
Enter first number: 53.3 Enter second number: 42.2 After swapping, first number = 42.2 After swapping, second number = 53.3