C PROGRAM TO SWAP TWO NUMBERS.
C Program to swap two numbers.
(Entered by User)
#include<stdio.h>
int main() {
double n1, n2, temp;
printf("Enter First Number: ");
scanf("%lf", &n1);
printf("Enter Second Number: ");
scanf("%lf", &n2);
// Value of first is assigned to temp
temp = n1;
// Value of second is assigned to first
n1 = n2;
// Value of temp (initial value of first) is assigned to second
n2 = temp;
printf("\nAfter swapping, First Number = %.2lf\n", n1);
printf("After swapping, Second Number = %.2lf", n2);
return 0;
}
Output
Enter First Number: 23.77 Enter Second Number: 1.02 After swapping, First Number = 1.02 After swapping, Second Number = 23.77
Explanation
- In above example temp variable is assigned to n1 variable.
- Then the value of n1 variable is assigned to n2 variable.
- At last, the temp (holding value of n1) is assigned to n2 variable.
- This completes the swapping process.
Comments
Post a Comment