What is the C program to swap two numbers without using third variable?
Example: x=10 y=40
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
You must login to ask a question.
What is the C program to swap two numbers without using third variable?
Example: x=10 y=40
#include<stdio.h>
int main() { int x=10,y=40; printf("Before swap \nx=%d y=%d",x,y); x=x+y; y=x-y; x=x-y; printf("After swap \nx=%d and y=%d",x,y); return 0; }
HAVE A LOOK AT ATTACHMENT FOR AN ANSWER:
Algorithm to the C program to swap two numbers without using third variable:
1. Start
2. Declare two integer variables x and y and initialize them with the values 10 and 40 respectively.
3. Print the values of x and y before swapping using the printf statement
4. Add the value of x to y and store it in x.
5. Subtract the original value of y from the new value of x and store it in y
6. Subtract the new value of y from the new value of x and store it in x
7. Print the new values of x and y after swapping using the printf statement
8. End