C Program to Swap Two Numbers

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

  1. Include the Standard Input-Output Library: Use #include <stdio.h> to include the standard input-output library, which is necessary for using printf and scanf functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare variables to store the two numbers and a temporary variable for swapping.
  4. Input the Numbers: Use scanf to take input from the user for the two numbers.
  5. Swap the Numbers: Use a temporary variable to swap the values of the two numbers.
  6. Display the Swapped Values: Use printf to 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 a and b are declared to store the two numbers. A temporary variable temp is also declared to help with swapping.

Step 2: Input the First Number

  • The program prompts the user to enter the first number using printf. The scanf function then reads the input and stores it in the variable a.

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 a is stored in the temporary variable temp.
  • The value of b is then assigned to a.
  • Finally, the value stored in temp (original value of a) is assigned to b, completing the swap.

Step 5: Display the Swapped Values

  • The swapped values of a and b are displayed using the printf function.

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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top