Swap Numbers in C

Exercise:

Write a C program to swap numbers.

1. Swap Using Temporary Variable

Click Here to View the Solution!
#include<stdio.h>
int main() {
    double firstNum, secondNum, temp;
    printf("Enter first number: ");
    scanf("%lf", & firstNum);
    printf("Enter second number: ");
    scanf("%lf", & secondNum);

    // Value of firstNum is assigned to temp
    temp = firstNum;

    // Value of secondNum is assigned to firstNum
    firstNum = secondNum;

    // Value of temp is assigned to second
    secondNum = temp;
    printf("\nAfter swapping, first Number = %.2lf\n", firstNum);
    printf("After swapping, second Number = %.2lf", secondNum);
    return 0;
}
Click Here to View the Output!
Enter first number: 6
Enter second number: 8
After swapping, first Number = 8.00
After swapping, second Number = 6.00
Click Here to View the Explanation!
  • This program is used to exchange the places of two numbers with one another using a temporary variable.
  • In the main method, the program requests the user to enter two floating-point numbers and stores them in the variables firstNum and secondNum using the scanf() function.
  • A variable temp is used which will initially store the value of firstNum.
  • The value of secondNum is then stored in the variable firstNum.
  • Finally, the value of temp is stored in the variable secondNum. Thus, swapping the numbers in the two variables.
  • The new swapped values of the variables are printed on the screen using the printf() function
  • The return 0 statement is used to exit the program execution.

2. Swap Without Using Temporary Variable

Click Here to View the Solution!
#include <stdio.h>  
int main() {
    double firstNum, secondNum;
    printf("Enter First Number: ");
    scanf("%lf", &firstNum);
    printf("Enter Second Number: ");
    scanf("%lf", &secondNum);

     // Swapping Procedure
     // firstNum = (initialFirstNum - initialSecondNum)
     firstNum = firstNum - secondNum;

     //secondNum = (initialFirstNum -initialSecondNum) + initialSecondNum= initialFirstNum 
     secondNum = firstNum + secondNum;

    // firstNum = initialFirstNum - (initialFirstNum - initialSecondNum) = initialSecondNum
    firstNum = secondNum - firstNum;
    printf("After swapping, First Number= %.2lf\n", firstNum);
    printf("After swapping, Second Number = %.2lf", secondNum);
    return 0;
}
Click Here to View the Output!
Enter First Number: 4
Enter Second Number: 7 
After swapping, First Number= 7.00
After swapping, Second Number = 4.00  
Click Here to View the Explanation!
  • This program is used to exchange the places of two numbers with one another without using a temporary variable.
  • In the main method, the program requests the user to enter two floating-point numbers and stores them in the variables firstNum and secondNum using the scanf() function.
  • Initially, the value in secondNum is subtracted from firstNum and the result is stored in firstNum.
  • Then, secondNum is added into the new a value and the result is stored in secondNum.
  • Finally, the value of firstNum is subtracted from the new value of secondNum and the result is stored in firstNum producing a new value of firstNum. This swaps the two values between firstNum and secondNum.
  • The new swapped values of the variables are printed on the screen using the printf() function.
  • The return 0 statement is used to exit the program execution.

%d bloggers like this: