Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to exchange two numbers in C language?
How to exchange two numbers in C language?
Method 1: (introducing variables)

It can be thought that A, B and C each occupy a small cell, and the numbers can be put in the small cell. The specific steps are as follows:

The process is as follows:

# include & ltstdio.h & gt

int main()

{

int a,b,c;

Scanf("%d %d ",& a, & b);

c = a;

a = b;

b = c;

printf("a=%d b=%d\n ",a,b);

Returns 0;

}

Method 2: ()

The exchange of two numbers can be expressed by the formula:

a = b-a; b = b-a; a = b+ a; To achieve.

The process is as follows:

# include & ltstdio.h & gt

int main()

{

int a,b;

scanf("%d %d ",a,b);

a = a+b;

b = a-b;

a = a-b;

printf("a=%d,b=%d ",a,b);

Returns 0;

}

Method 3: (Pointer)

The process is as follows:

# include & ltstdio.h & gt

int fun(int *p,int *q)

{

int tmp = * p;

* p = * q;

* q = tmp

}

int main()

{

int a,b;

Scanf("%d %d ",& a, & b);

printf("%d,%d\n ",a,b);

Fun (&one, & b);

printf("%d,%d\n ",a,b);

Returns 0;

}