This C program will swap the two entered numbers by using the temporary variable. This is a simple & easy program in the programming language. See the below example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <stdio.h> int main() { double firstNumber, secondNumber, temporaryVariable; printf("Enter first number: "); scanf("%lf", &firstNumber); printf("Enter second number: "); scanf("%lf",&secondNumber); // Value of firstNumber is assigned to temporaryVariable temporaryVariable = firstNumber; // Value of secondNumber is assigned to firstNumber firstNumber = secondNumber; // Value of temporaryVariable (which contains the initial value of firstNumber) is assigned to secondNumber secondNumber = temporaryVariable; printf("\nAfter swapping, first number = %.2lf\n", firstNumber); printf("After swapping, second number = %.2lf", secondNumber); return 0; } |
1 2 3 4 5 |
Enter first number: 40 Enter second number: 50 After swapping, first number = 50 After swapping, second number = 40 |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: c program examples, c program for swap of two numbers, c tutorials, program to swap two numbers in c, swap two numbers in c