Introduction
Swapping two numbers involves exchanging the values of the two variables. This guide will show you how to write a simple C program to swap the values of two variables using a temporary variable.
Problem Statement
Create a C program that:
- Takes two numbers as input from the user.
- Swaps the values of the two numbers.
- Displays the numbers after swapping.
Example:
- Input:
a = 5,b = 10 - Output:
a = 10,b = 5
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>to include the standard input-output library, which is necessary for usingprintfandscanffunctions. - Write the Main Function: Define the
mainfunction, which is the entry point of every C program. - Declare Variables: Declare variables to store the two numbers and a temporary variable for swapping.
- Input the Numbers: Use
scanfto take input from the user for the two numbers. - Swap the Numbers: Use a temporary variable to swap the values of the two numbers.
- Display the Swapped Values: Use
printfto display the numbers after swapping.
C Program
#include <stdio.h>
/**
* C Program to Swap Two Numbers
* Author: https://www.javaguides.net/
*/
int main() {
// Step 1: Declare variables to hold the numbers and a temporary variable
int a, b, temp;
// Step 2: Prompt the user to enter the first number
printf("Enter the first number (a): ");
scanf("%d", &a);
// Step 3: Prompt the user to enter the second number
printf("Enter the second number (b): ");
scanf("%d", &b);
// Step 4: Swap the numbers using a temporary variable
temp = a; // Store the value of a in temp
a = b; // Assign the value of b to a
b = temp; // Assign the value of temp (original a) to b
// Step 5: Display the swapped values
printf("After swapping, a = %d, b = %d\n", a, b);
return 0; // Step 6: Return 0 to indicate successful execution
}
Explanation
Step 1: Declare Variables
- Variables
aandbare declared to store the two numbers. A temporary variabletempis also declared to help with swapping.
Step 2: Input the First Number
- The program prompts the user to enter the first number using
printf. Thescanffunction then reads the input and stores it in the variablea.
Step 3: Input the Second Number
- Similarly, the program prompts the user to enter the second number, which is stored in the variable
b.
Step 4: Swap the Numbers
- The value of
ais stored in the temporary variabletemp. - The value of
bis then assigned toa. - Finally, the value stored in
temp(original value ofa) is assigned tob, completing the swap.
Step 5: Display the Swapped Values
- The swapped values of
aandbare displayed using theprintffunction.
Step 6: Return 0
- The
return 0;statement indicates that the program executed successfully.
Output Example
Example:
Enter the first number (a): 5
Enter the second number (b): 10
After swapping, a = 10, b = 5
Conclusion
This C program demonstrates how to swap the values of two variables using a temporary variable. It covers basic concepts such as variable declaration, taking user input, and using a temporary variable for swapping, making it a useful example for beginners learning C programming.