Skip to main content

c program for swap two numbers by two ways

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




     

Comments

Popular posts from this blog

Interior designing -best option for career.

Today we live in that era where there is a lot of career options one of them is interior designing. Now let us know about interior designing so what actually interior designing is  Interior designing means enhancing of interiors of houses and building by art and technical knowledge. Interior designing is a multifaceted profession (which means it include a wide range of profession in itself) that include 1.  t echnical and conceptual development of the individual, 2. i t helps in the planning of the space inside a room of a building or a house 3.  i t also include site inspection, programming and research. 4. i t also help in communicating with the stakeholders of a project, construction management,execution of the design. A HISTORICAL e.g . : BALLET COLLEGE DINNING HALL Now we will see the basic difference between interior designing and interior decorators . So the basic difference between interior designing and interior decorators is education  , a...

How nanobots are used as artificial blood?

Nanobots are actually nano-robots  , those robots whose parts are in the size of nanometers. The application of these nanobots is this that they can be used in those areas where it is difficult for human hands. Since now in human body artificial blood can be used which is made of these nanobots . Just like human blood which consist of 1. Red blood cells 2. White blood cells 3. Platelets Function of RED BLOOD CELLS 1. Haemoglobin transports about 97-98% of oxygen from lungs to body tissues as oxyhaemoglobin. 2. Haemoglobin also transports about 23% of carbon dioxide from the body tissues to the lungs as carbaminohaemoglobin. FUNCTION OF WHITE BLOOD CELLS 1. Neutrophils act as soldiers gaurding the body . They eat up invading microbes by phagocytosis.  2. Basophils secrete heparin which prevents coagulation of blood in the blood vessels. 3. Monocytes act as scavengers and eat up damaged and dead cells to keep the body clean. 4. Acidophils help in healing of...