program to swap 2 number without using 3rd variable


program to swap 2 number without using 3rd variable

Hello friends today we are going to make a program to swap two numbers without using third variable is a simple program but with different logic this question is very commonly asked in the placement when you are going for the interview you must prepare this question but with different conditions like using ternary operators are not using third variable or by the function .


It is generally found that it is one of the most favourite question of the interviewer so be ready with this program if you are visiting a recruitment.


Let's understand what is the swapping means swapping is simply replacing the content of one variable with the other but how to do that the main question is that?
For this first understand the algorithm how to perform this action and a detailed note and a source code has been provided on the blog to help you out.

How to swap two numbers without using the 3rd number?
the concept behind is to get sm in one variable of the two given numbers.from the sum numbers can be easily swapped using the sum and subtraction from the sum
c4fun  provides you the basic program in a simplified manner which can be easily understood by the students
A source code has been provided below for you to help you out in building a program which swap two numbers without using third variable but I recommend you to first build yourself the program without looking this source code if you are struck anywhere then go for the source code it help you to boost your confidence in you..


#include<stdio.h>
#include<conio.h>
void main()
{
int a=2,b=3;
clrscr();
printf("values before swapping\n");
printf("a=%d\tb=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("value after swapping\n");
printf("a=%d\tb=%d",a,b);
getch();
}

output
value before swapping
a=2 b=3
the value after swapping
a=3 b=2



The source code you have I hope you have implemented it and use the program to find out the result now when you have the result and the knowledge make some changes in your program according to you try different inputs and share the result you have obtained.
Post a comment in the comment section if you have any questions or the query about the code of the functional how this code run . They would be help in no time.



Share this code and program blog among your friends and help them too.
Share this code with the person whom you think would help him to build up confidence is small small programs make it a large difference.



Keep sharing and keep coding as C for fun is based on the sharing is caring concept when you share our blog with the person he share with other He share with others and this goes and on and on.



Keep learning
Keep coding
And keep updating yourself with the latest C programs as she is developing everyday if you are left Behind then you are lost in the race....

No comments

thanks for reading and sharing.I hope it would help you!!!!

Powered by Blogger.