Menu
×
Tutorials Ms Word Tutorial Ms Excel Tutorial Ms PowerPoint Tutorial C Language Tutorial C++ Tutorial C Sharp Tutorial Visual Basic Tutorial HTML Tutorial CSS Tutorial JavaScript Tutorial WordPress Tutorial
     ❯   

ADVERTISEMENT

Swap Two Numbers Using 3rd Variable

This C program takes two integer inputs, ‘a’ and ‘b’, and swaps their values using a temporary variable ‘c’.

#include<stdio.h>
main()
{
 int a,b,c;
 printf("Enter a\n");
 scanf("%d",&a);
 printf("Enter b\n");
 scanf("%d",&b);
 printf("The values of a is %d and b is %d before swaping\n",a,b);
 c=a;
 a=b;
 b=c;
 printf("The values of a is %d and b is %d after swaping\n",a,b);
}
C Programming Examples

OUTPUT
Enter a
5
Enter b
3
The values of a is 5 and b is 3 before swaping
The values of a is 3 and b is 5 after swaping
C Programming Examples

Explanation:

Here’s a step-by-step explanation of the code:

Header File:

  • The program includes the standard C library header <stdio.h>, which is required for input and output operations.

Main Function:

  • The main function is the entry point of the program, where the execution starts.
  • Variable Declarations:
  • The program declares three integer variables: ‘a’, ‘b’, and ‘c’. ‘a’ and ‘b’ are used to store the values provided by the user, and ‘c’ is a temporary variable to facilitate the swapping process.

User Input:

  • The program prompts the user to enter the value of ‘a’ with the message “Enter a” and waits for the user’s input.
  • The scanf function is used to read the input, and the value is stored in the variable ‘a’.
  • Similarly, the program prompts the user to enter the value of ‘b’ and reads the input, storing it in the variable ‘b’.

Display Values Before Swapping:

  • The program uses printf to display the values of ‘a’ and ‘b before swapping.
  • The message “The values of a is %d and b is %d before swaping” is used to display the initial values of ‘a’ and ‘b. The placeholders %d are replaced with the actual values of ‘a’ and ‘b when the printf function is executed.

Swapping with a Temporary Variable:

  • The code performs the swap using a temporary variable ‘c’:
  • c = a: The value of ‘a’ is assigned to ‘c’, effectively storing ‘a’ in the temporary variable.
  • a = b: The value of ‘b’ is assigned to ‘a’, overwriting the value of ‘a’ with ‘b’.
  • b = c: The value stored in ‘c’ (which was the original ‘a’ value) is assigned to ‘b’, completing the swap. Now, ‘a’ holds the original value of ‘b’, and ‘b’ holds the original value of ‘a’.

Display Values After Swapping:

  • The program uses printf to display the values of ‘a’ and ‘b after swapping.
  • The message “The values of a is %d and b is %d after swaping” is used to display the updated values of ‘a’ and ‘b after the swap.
  • The placeholders %d are replaced with the actual values of ‘a’ and ‘b when the printf function is executed.

End of Program:

  • After displaying the swapped values, the program reaches the end of the main function and, consequently, the end of the program’s execution.

This code allows the user to enter two integer values, ‘a’ and ‘b’, and then swaps their values using a temporary variable ‘c’. It demonstrates a common method for swapping variables in programming.

Spread the love
Scroll to Top
Scroll to Top