Write a C program to swap two numbers by using a temporary variable, In this C program example, we will see how to swap two numbers using a temporary variable.
sample input
enter the value of a: 25
enter the value of b: 52
sample output
52 25
Algorithm to swap two numbers using with temporary variable
- Declare three variables.
- Then take two inputs for a and b.
- Give the logic of swap number.
- print with appropriate message.
C program to swap two numbers using a temporary variable
#include<stdio.h> void main (){ int a,b,t; printf("enter the value of a: "); scanf("%d",&a); printf("enter the value of b: "); scanf("%d",&b); t=a; a=b; b=t; printf("%d %d",a,b); }
Explanation of this C program
Step 1: Start.
Step 2: Create a header file and include a library on file and create a void main function.
Step 3: Declare three variables (a,b,t).
Step 4: Then, create two inputs for a and b to taking values of a and b, with the help of scanf.
Step 5: Create logic for swap numbers.
Step 6: Print with an appropriate message.
Step 7: End.
Output
In this way, we will see how to write a C program to swap two numbers by using a temporary variable.