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:

  1. #include<stdio.h>
  2. int main() {
  3. double first, second, temp;
  4. printf("Enter first number: ");
  5. scanf("%lf", &first);
  6. printf("Enter second number: ");
  7. scanf("%lf", &second);
  8. // Value of first number assigned into temp
  9. temp = first;
  10. // Value of second is assigned to first
  11. first = second;
  12. // Value of temp is assigned to second
  13. second = temp;
  14. printf("\nAfter swapping, first number = %.2lf\n", first);
  15. printf("After swapping, second number = %.2lf", second);
  16. return 0;
  17. }

Output:

Enter first number: 53.3
Enter second number: 42.2

After swapping, first number = 42.2
After swapping, second number = 53.3