数位拆分v2.0(4分)
题目内容:
从键盘上输入一个4位数的整数n,编写程序将其拆分为两个2位数的整数a和b,计算并输出拆分后的两个数的加、减、乘、除和求余运算的结果。例如n=-4321,设拆分后的两个整数为a,b,则a=-43,b=-21。除法运算结果要求精确到小数点后2位,数据类型为float。求余和除法运算需要考虑除数为0的情况,即如果拆分后b=0,则输出提示信息"The second operater is zero!"
程序的运行结果示例1:
Please input n:
1200↙
12,0
sum=12,sub=12,multi=0
The second operator is zero!
程序的运行结果示例2:
Please input n:
-2304↙
-23,-4
sum=-27,sub=-19,multi=92
dev=5.75,mod=-3
输入提示信息:"Please input n:\n"
输入格式: "%d"
输出格式:
拆分后的两个整数的输出格式:"%d,%d\n"
加法、减法、乘法的输出格式:"sum=%d,sub=%d,multi=%d\n"
除法和求余的输出格式:"dev=%.2f,mod=%d\n"
除数为0的提示信息:"The second operator is zero!\n"
#include <stdio.h>
int main()
{
int a,b,n,sum,sub,multi,mod;
float dev;
printf("Please input n:\n");
scanf( "%d",&n);
a=n/100;
b=n%100;
sum=a+b;
sub=a-b;
multi=a*b;
dev=(float)a/b;
mod=a%b;
printf("%d,%d\n",a,b);
printf("sum=%d,sub=%d,multi=%d\n",sum,sub,multi);
if(b==0){
printf("The second operator is zero!\n");
}else{
printf("dev=%.2f,mod=%d\n",dev,mod);}
return 0;
}