输入两个整数,交换后输出 C语言怎么编程

如题所述

利用C语言来实现交换两个变量的值,需要定义三个变量:

#include<stdio.h>

int main()

{

int x,y,temp;//定义三个变量

printf("请输入分别x和y的值\n");

scanf("%d  %d",&x,&y);//终端输入变量x、y

temp=y;//把y赋值给temp

y=x;//把x赋值给y

x=temp;//把y赋值给temp

printf("%d %d",x,y);//输出交换后x和y的值

return 0;

}

结果如下图所示:

扩展资料

其他解决该问题的方法

需要定义两个变:

#include<stdio.h>

int main()

{

int x,y;//定义两个变量

printf("请输入分别x和y的值\n");

scanf("%d  %d",&x,&y);//终端输入变量x、y

x=y-x;

y=y-x;//把x赋值给y

x=y+x;//把y赋值给x

printf("%d %d",x,y);//输出交换后x和y的值

return 0;

}

温馨提示:内容为网友见解,仅供参考
第1个回答  2013-10-22
你看看,好长时间没接触C语言了
int a,b,c;
printf("输入两个整数:\n");
scanf("%d %d",&a,&b);
c=a;
a=b;
b=c;
printf("输出为:%d %d",a,b);
第2个回答  推荐于2018-02-26
#include<stdio.h>
int main(){
int a,b,t;
scanf("%d%d", &a,&b);
t=a;
a=b;
b=t;
printf("%d %d", a, b);
return 0;
}本回答被网友采纳
第3个回答  2013-10-22
代码如下:#include<stdio.h>int main(){ int a, b; scanf("%d%d", &a, &b); printf("%d %d", b, a); return 0; }
第4个回答  2018-05-23
#include <stdio.h>
void swapnum(int *x, int *y);
main(int argc, char* argv[]) {
int a,b;
printf("Please input number a and number b: ");
scanf("%d %d",&a, &b);
swapnum(&a, &b);
printf("The number changed is %d, %d\n", a,b);
return 0;
}
void swapnum(int *x, int *y) {
int t;
t=*x, *x=*y, *y=t;
}
相似回答