在C / C ++中交换两个变量值而不使用第三个变量
以下是交换两个变量的示例。
示例
#include <stdio.h>
int main() {
int a,b;
printf("Enter the value of a : ");
scanf("%d", &a);
printf("\nEnter the value of b : ");
scanf("%d", &b);
a += b -= a = b - a;
printf("\nAfter Swapping : %d\t%d", a, b);
return 0;
}输出结果
Enter the value of a : 23 Enter the value of b : 43 After Swapping : 4323
在上面的程序中,声明了两个变量a和b,并在运行时对其进行了动态初始化。
int a,b;
printf("Enter the value of a : ");
scanf("%d", &a);
printf("\nEnter the value of b : ");
scanf("%d", &b);交换数字时不使用任何第三个变量。
a += b -= a = b - a;
热门推荐
10 同学有弟弟祝福语简短
11 校长退休文案祝福语简短
12 孩子懂事生日祝福语简短
13 儿媳买车妈妈祝福语简短
14 诗词送行祝福语大全简短
15 远方同事回家祝福语简短
16 M退休祝福语结婚简短
17 男士送花祝福语大全简短
18 给哥嫂结婚祝福语简短