Place Your Query
-
C Programming
-
- Control flow-based C Programs
- Enjoy Conditional Programming in C using If...else and switch statement.
- Good, Now write a C program to display "Hello World" on the screen.
- Write a C program to display Your Name, Address and City in different lines.
- C program to find sum of n numbers
- Write a C program For Addition of two numbers using Function.
- Write a Program to Find Simple Interest and Compound Interest.
- Write a Program to Convert Celsius To Fahrenheit and Vice Versa.
- Write a Program to Find Area and Perimeter of Circle, Triangle, Rectangle and Square.
- Write a Program to Swap Two Numbers Using Temporary Variables and Without Using Temporary Variables
- Write a C Program to Design a Simple Menu Driven Calculator
- Simple Expression Based C Programs
-
- 7. Components of C language
- 1. Introduction to C Programming Language
- 10. Operator Precedence and Associativity
- 11. Comments in C Programming
- 14. Fundamental Control Structure Statements in C [Part-1]
- 15. Fundamental Control Structure Statements in C [Part-2]
- 16. Looping Statements [Fundamental Control Structure Statements in C. #Part-3]
- 17. Keyword break, continue, return and exit [Fundamental Control Structure Statements in C. #Part-4]
- 2. Computer Languages
- 3. Interpreters vs Compilers vs Assemblers in programming languages
- 4. C Program Structure
- 5. Compile and Execute C Program
- 6. Errors in C Program
- 8. C Datatypes
- 9. Operators in C
- Control flow-based C Programs
- Demystifying Bit Masking: Unlocking the Power of Bitwise Operators in C-Language with best Examples.
- 18. Fundamentals of C Functions
- Show Remaining Articles (3)Collapse Articles
-
-
Java
-
AI ML
-
FAQs
- A Program to find Tokens in C code?
- What are the Common Coding Mistakes.
- Easy Learning, Python QAs for Beginner’s to Pro Part-1
- Easy Learning, Python QAs for Beginner’s to Pro Part-2
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-1
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-2
- Easy Learning, Python String Functions QAs for Beginner to Pro Part-3
-
Python Interview Questions
- Easy Learning, Python QAs for Beginner’s to Pro Part-1
- Easy Learning, Python QAs for Beginner’s to Pro Part-2
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-1
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-2
- Easy Learning, Python String Functions QAs for Beginner to Pro Part-3
Write a Program to Swap Two Numbers Using Temporary Variables and Without Using Temporary Variables
Aim
To write a C program to swap two number a) using temporary variable and b) without using temporary variable and c) without using temporary variable and use bitwise operator.
In these three ways, one can easily write a program to swap two numbers using temporary variables and without using temporary variables.
Procedure
Step-1: Start the program execution
Step-2: Declare num1, num2, temp.
Step-3: Read 2 values num1 and num2.
Step-4: A) using temporary variable: temp=num1, num1=num2, num2=temp;
Print the values of num1 and num2.
Step-5: B)without using temporary variable: num1= num1+num2, num2= num1-num2, num2=num1-num2.
Print the values of num1 and num2.
Step-6: C) without using temporary variable but apply XoR operator:
num1= num1+num2, num2= num1^num2, num2^num1^num2.
Print the values of num1 and num2.
Step-6: Stop the program execution.
Coding
A. By using temporary variable.
// using third variable as temp.
// author milind bhatt
#include<stdio.h>
int main()
{
int num1, num2, temp;
printf("\nEnter two values:\n");
scanf("%d%d",&num1,&num2);
printf("\nSwapping with temporary variables:\n\n");
printf("\nBefore swapping the values are num1 = %d and num2 =%d",num1,num2);
temp=num1;
num1=num2;
num2=temp;
printf("\nAfter swapping the values are num1=%d and num2=%d",num1,num2);
return 0;
}// end main
Output
Enter two values:
12
14
Swapping with temporary variables:
Before swapping the values are num1 = 12 and num2 =14
After swapping the values are num1=14 and num2=12
--------------------------------
B. Without using temporary variable.
// without using third variable as temp.
// author milind bhatt
#include<stdio.h>
int main()
{
int num1, num2, temp;
printf("\nEnter two values:\n");
scanf("%d%d",&num1,&num2);
printf("\nSwapping with temporary variables:\n\n");
printf("\nBefore swapping the values are num1 = %d and num2 =%d",num1,num2);
num1=num1+num2;
num2=num1-num2;
num1=num1-num2;
printf("\nAfter swapping the values are num1=%d and num2=%d",num1,num2);
return 0;
}// end main
Enter two values:
12
68
Swapping with temporary variables:
Before swapping the values are num1 = 12 and num2 =68
After swapping the values are num1=68 and num2=12
--------------------------------
C. By using bitwise XOR operator.
// without using third variable and using btwise operator
// author milind bhatt
#include<stdio.h>
int main()
{
int num1, num2, temp;
printf("\nEnter two values:\n");
scanf("%d%d",&num1,&num2);
printf("\nSwapping with temporary variables:\n\n");
printf("\nBefore swapping the values are num1 = %d and num2 =%d",num1,num2);
//swapping two numbers without third variable and usng bitwise operator
num1=num1^num2;
num2=num1^num2;
num1=num1^num2;
printf("\nAfter swapping the values are num1=%d and num2=%d",num1,num2);
return 0;
}// end main
Output
Enter two values:
10
20
Swapping with temporary variables:
Before swapping the values are num1 = 10 and num2 =20
After swapping the values are num1=20 and num2=10
--------------------------------
Run Directly on in-built C-Compiler
While using in-built C-Compiler Use Gear icon for custom Input (i.e. whenever you have to give values to read)
RESULT
Thus, the C program has been written to swap two variables using temporary variable and without using temporary variable. These are the three major ways to swap to numbers. Most of the times one can use any single method but in case of smart coding I will suggest using a programming approach of without third variable.