C programs

C Program for swap of two Numbers by using only two variables

In this program we swap value of two integers using  only two variable. We take values from user at runtime applies our logic to swap and then printed the swapped value on screen. To see program on swap two variable value using three variable click here

Program

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b;

clrscr();

printf("enter the first no:");                                //variable declaration

scanf("%d",&a);                                                  //address of 1st no.s

printf("enter the second no:");                           //variable declaration

scanf("%d",&b);                                                //address of 2nd no.s

a=a-b;

b=a+b;

a=b-a;

printf("first no.=%d\nsecond no.=%d",a,b);    //print reult of swapping no.s

getch();

}

When Run The Program Following Output:

enter the first no:18

enter the second no:39

first no.=39

second no.=18

Description

In above program we swapped value of two variable using only those two variable. Using the logic

a=a-b;

b=a+b;

a=b-a;

To see program on swap two variable value using three variable click here

Releted Programs

© 2017 codewmAll rights reserved.