HOW TO SWAP TWO NUMBERS IN C LANGUAGE

Here we will learn how to swap two numbers in C Language.

There are the different way to swap two numbers in C Language.

1.By using / and * operator

2.By using Third variable

3.By using without the Third variable

Now we will discuss one by one.So Let’s Start.

1.By using / and * operator

#include<stdio.h>

void main()

{

int a=5,b=10;// Here we have taken two variable a and b which values are 5 and 10 respectively.

a=a*b;// Here a value will be=5*10=50

b=a/b;// Here a value will be 50(updated/new value) ie 50/10=5

a=a/b;// Here b value will be 5(updated/new value) ie 50/5=10

printf(“a value will be=%d b value will be=%d”,a,b);

}

Output:

a value will be=10 b value will be=5

2.By using Third variable

#include<stdio.h>

void main()

{

int a=5,b=10,swap;

swap=a;//Here swap value will be” 5″ ie a value

a=b; //Here a value will be 10 ie “b” value

b=swap; //Here b value will be 5 ie “swap” value

printf(“a value will be=%d b value will be=%d”,a,b);

}


Output:

a value will be =10 b value will be= 5

3 By using without Third variable

#include<stdio.h>

void main()

{
int a=5,b=10;

a=a+b; // Here a value will be 5+10=15

b=a-b; //Here b value will be 15-10=5 ie a(updated value)-b(updated value)

a=a-b; // Here a value will be15-5=10 ie a(updated value)-b(updated value)

printf(“a value will be=%d b value will be %d”,a,b);

}

Output:

a value will be=10 b value will be 5





sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *