www.fltk.net > 用C语言设计一个计算两个整数的和、差、积、商、和余数的程序

用C语言设计一个计算两个整数的和、差、积、商、和余数的程序

#include int main() { int x1,x2,sum,cha,ji,shang,yushu; printf("Please input Num[1]\n"); scanf("%d",&x1); printf("Please input Num[2]\n"); scanf("%d",&x2); sum=x1+x2; printf("sum = %d\n",sum); cha=x1-x2; printf("cha = %d\n",cha); ji=x

#include <stdio.h> int main(void) { int num1, num2; scanf("%d%d",&num1,&num2); printf("%d + %d = %d\n",num1,num2,num1+num2); printf("%d - %d = %d\n", num1, num2, num1-num2); printf("%d * %d = %d\n", num1, num2, num1*

被除数不能为0,所以当num2=0的时候,程序就报错了. 还有,你两个数都是int型的.商是double型,在处理的时候,加一个强制转换shang=(double)num1/num2;这样就可以了

#include <stdio.h> void main(){ int a,b; printf("Input two integers:"); scanf("%d%d",&a,&b); printf("和:a+b=%d\n",a+b); printf("差:a-b=%d\n",a-b); printf("积:a*b=%d\n",a*b); printf("商:a/b=%d\n",a/b); printf("余数:a%b=%d\n",a%b); }

前面声明变量的时候类型是浮点型,所以格式符应该使用%f而不是%d,%d是整型.改为:#include "stdio.h" void main(){ float x,y,a,b,c,d;scanf("%f%f,&x,&y); printf("%f%f%f%f%f",x+y,x-y,x*y,x/y,x%d); } 扩展资料:printf函数格式字符:常用

// ForPEE.cpp : 定义控制台应用程序的入口点

#include<stdio.h>void main(){int a,b;float m;scanf("%d%d",&a,&b);printf("he shi %d",a+b);printf("cha shi %d",a-b);printf("ji shi %d",a*b);if(b!=0){m=shang shi a/b;printf("%f",m);}else printf("error");}我们没有进行调试,,可能有些有点问题,但总体是没错了,我把b当作是除数,就是说,后面一个数若为0的话,会输出eroor错误提示

#include <stdio.h>int main() {int a,b,c,d,e,f;printf("请输入一个整数:");scanf("%d",&a);printf("请再输入一个整数:");scanf("%d",&b);c = a+b;d = a-b;e = a*b;f = a/b;printf("两数和是%d,差是%d,积是%d,商是%d.", c, d, e, f);return 0; }

上面答案没有数据输入语句scanf,且麻烦, 改后如下: #include<stdio.h>int main(){ int m,n; printf("请输入整数m="); scanf("%d",&m); printf("请输入整数n="); // 输入m,n scanf("%d",&n); printf("和=%d\n",m+n); printf("

上面答案没有数据输入语句scanf,且麻烦, 改后如下:#include<stdio.h> int main() { int m,n; printf("请输入整数m="); scanf("%d",&m); printf("请输入整数n="); // 输入m,n scanf("%d",&n); printf("和=%d\n",m+n); printf(

网站地图

All rights reserved Powered by www.fltk.net

copyright ©right 2010-2021。
www.fltk.net内容来自网络,如有侵犯请联系客服。zhit325@qq.com