We can write the c program for swap in two ways:
1. by using only two variable,
2. by using three variable.
In first we will discuss our first type program i.e. by using only two variable. in this program only two variables are used to swap two numbers, in order to write a program we need to build a logic and after that the program will run.
1.firstly try to solve this program on your own
2.after this try to understand the problem i.e. to swap two numbers
3.now build a logic between two numbers
a=1,b=2
if i write a=b & b=a
this is WRONG way.
why this is wrong? A question comes in mind. This is due to the reason that when a=b (means the value in b is assigned to a) then the value in a will vanish. Now when we wite b=a(means the value in a is assigned to b) but from the above line we came to know that the value of a is vanished and the the value which is in a is of b.
so the proper way of making a relation between swaping of a and b is in the program.
The program for swap using only two variables is written given below
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("enter the value of a &b");
scanf("%d%d",&a,&b);
printf("before swap a=%d",a);
printf("before swap b=%d",b);
a=a+b;
b=b-a;
a=b-a;
printf("after swap a=%d",a);
printf("after swap b=%d',b);
getch();
}
RESULT: The example of input and output of the program is as under
METHOD 2:
by using three variable:
in order to solve this program we need to think that we have three variable and how to use these variable?
1. firstly store the value of first variable into third.
2. then store the value of second variable into first.
3. and then store the value of third variable into second.
This is how we will swap two numbers i.e. first and second using third variable now program for swap using three variables is given below:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("enter the value of a &b");
scanf("%d%d",&a,&b);
printf("before swap a =%d",a);
printf(before swap b=%d",b);
c=a;
a=b;
b=c;
printf("after swap a =%d",a);
printf("after swap b=%d",b);
getch();
}
RESULT: The example of input and output for this program is as under
by sudhanshu tidyal
1. by using only two variable,
2. by using three variable.
In first we will discuss our first type program i.e. by using only two variable. in this program only two variables are used to swap two numbers, in order to write a program we need to build a logic and after that the program will run.
1.firstly try to solve this program on your own
2.after this try to understand the problem i.e. to swap two numbers
3.now build a logic between two numbers
a=1,b=2
if i write a=b & b=a
this is WRONG way.
why this is wrong? A question comes in mind. This is due to the reason that when a=b (means the value in b is assigned to a) then the value in a will vanish. Now when we wite b=a(means the value in a is assigned to b) but from the above line we came to know that the value of a is vanished and the the value which is in a is of b.
so the proper way of making a relation between swaping of a and b is in the program.
The program for swap using only two variables is written given below
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("enter the value of a &b");
scanf("%d%d",&a,&b);
printf("before swap a=%d",a);
printf("before swap b=%d",b);
a=a+b;
b=b-a;
a=b-a;
printf("after swap a=%d",a);
printf("after swap b=%d',b);
getch();
}
RESULT: The example of input and output of the program is as under
METHOD 2:
by using three variable:
in order to solve this program we need to think that we have three variable and how to use these variable?
1. firstly store the value of first variable into third.
2. then store the value of second variable into first.
3. and then store the value of third variable into second.
This is how we will swap two numbers i.e. first and second using third variable now program for swap using three variables is given below:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("enter the value of a &b");
scanf("%d%d",&a,&b);
printf("before swap a =%d",a);
printf(before swap b=%d",b);
c=a;
a=b;
b=c;
printf("after swap a =%d",a);
printf("after swap b=%d",b);
getch();
}
RESULT: The example of input and output for this program is as under
by sudhanshu tidyal
Comments
Post a Comment