Write a program to swap values of two variables by using third variable.
TI
Answers
Answer:
HEY MATE HERE'S YOUR ANSWER
Step-by-step explanation:
ariable
1. Declare three variables.
2. i) Assign the value of the first variable in temp.
ii) Then assign the value of the second variable into the first variable.
iii) Finally, assign the value of temp variable into the second variable.
Let’s declared three variables temp, a and b. Code logic to swap two numbers using third variable.
- //Logic to swap two numbers using third variable
- temp = a;
- a = b;
- b = temp;
C Program to swap two numbers without using third variable
Swap two numbers using a pointer
Swap two numbers using bitwise XOR operator
Programming questions on various topics for practice
We have understood how to swap two numbers using third variable. Let’s write a c code to implement them.
- #include <stdio.h>
- int main(void)
- {
- // Variable declaration
- int a, b, temp;
- printf("Enter two numbers a and b ");
- scanf("%d %d", &a, &b);
- // Swap logic
- temp = a;
- a = b;
- b = temp;
- printf("\n After swapping \na = %d\nb = %d\n", a, b);
- return 0;
- }
C, C++ Programming Questions for Practice
HOPE IT WILL HELP YOU PLZ MRK AS BRAINLIST.
DON'T FORGET TO FØLLØW ME
HV A GOOD DAY