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 variablesfirstNum
andsecondNum
using thescanf()
function. - A variable
temp
is used which will initially store the value offirstNum
. - The value of
secondNum
is then stored in the variablefirstNum
. - Finally, the value of
temp
is stored in the variablesecondNum
. 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 variablesfirstNum
andsecondNum
using thescanf()
function. - Initially, the value in
secondNum
is subtracted fromfirstNum
and the result is stored infirstNum
. - Then,
secondNum
is added into the new a value and the result is stored insecondNum
. - Finally, the value of
firstNum
is subtracted from the new value ofsecondNum
and the result is stored infirstNum
producing a new value offirstNum
. This swaps the two values betweenfirstNum
andsecondNum
. - 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.