swapping two numbers in c

In this example, we take two input from user and swap their value with help of third variable.

C Program to Swap Two Numbers :

In this example, we will use temporary variable temp for swapping value of first and second variable. Here, temp variable has no other use rather than storing temporary value of other variable.

Example :

	
		#include
		int main() {
		    double first, second, temp;
		    printf("Enter first number: ");
		    scanf("%lf", &first);
		    printf("Enter second number: ");
		    scanf("%lf", &second);

		    temp = first;

		    first = second;

		    second = temp;

		    printf("\nAfter swapping, firstNumber = %.2lf\n", first);
		    printf("After swapping, secondNumber = %.2lf", second);
		    return 0;
		}
	

Output :

	
		Enter first number: 10
		Enter second number: 20
		After swapping, firstNumber = 20.00
		After swapping, secondNumber = 10.00
	

Share your thoughts

Ask anything about this examples