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

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...

Use your spare time in earning money through online apps

Now here are the 5 apps which you can use to earn money online 1. Flikk      It's an online app of 5.6 megabyte having a rating of 4.4 and downloaded by 5 million people overseas. Now with the help of this app you can perform alot of things with it's features some of them are: 1. It runs on two basic languages which are Hindi and English. 2. With this app you can watch live cricket scores, and stay updated with top stories . 3. It also give gureanteed mobile recharges up to $1.5 every day. 2. Task   bucks     Its an online app of 7.2 megabyte having a rating of 4.4 and downloaded over 10 million overseas. This app gives us tasks and with the completion of tasks we can earn money as: 1. Get paytm cash everyday complete a task &get up to 💯 Rupees in a day. 2. The reward you earn it can be transferred to your paytm wallet. 3. It offers us with free recharges & bill payments by completing tasks. 4. You can play quiz and can win 50 r...