在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 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语