『壹』 用C語言編寫(計算兩個整數的和、 差、積、商與余數
代碼一:
#include<stdio.h>
int main(void)
{
int num1, num2;
scanf("%d%d",&num1,&num2);
printf("%d + %d = %d ",num1,num2,num1+num2);
printf("%d - %d = %d ", num1, num2, num1-num2);
printf("%d * %d = %d ", num1, num2, num1*num2);
if (num2) printf("%d / %d = %g ", num1, num2, (double)num1/num2);
else printf("ERROR ,DIVBYZERO ");
printf("%d %% %d = %d ", num1, num2, num1%num2);
return 0;
}
代碼二:
#include <stdio.h>
void main()
{
int x, y;
printf("please enter a,b,c:");
scanf("%d,%d",&x,&y);
printf(" x+y=%d ",x+y);
printf("x-y=%d ",x-y);
printf("x*y=%d ",x*y);
printf("x/y=%d ",x/y);
printf("x%%y=%d ",x%y);
}
下圖為C語言算術運算符:
算術運算符實例:
#include <stdio.h>
main()
{
int a = 21;
int b = 10;
int c ;
c = a + b;
printf("Line 1 - c 的值是 %d ", c );
c = a - b;
printf("Line 2 - c 的值是 %d ", c );
c = a * b;
printf("Line 3 - c 的值是 %d ", c );
c = a / b;
printf("Line 4 - c 的值是 %d ", c );
c = a % b;
printf("Line 5 - c 的值是 %d ", c );
c = a++;
printf("Line 6 - c 的值是 %d ", c );
c = a--;
printf("Line 7 - c 的值是 %d ", c );
}
『貳』 C語言編程 怎麼對兩個大整數進行求余啊
兩個
int
型數據可以用%取余
兩個
double
型小數,乘以相應的倍數轉成整形,取余,再除以相應的倍數,轉為double型
兩個double型大整數(int型放不下),可以自己編寫。先判斷兩數大小,用大數不斷減小數,直到差小於小數,此時差就是余數。當兩數差距較大(如10倍以上),可以一開始直接減小數的10倍加快速度,具體方法你自己想一想吧