在c中不使用除法运算符来除数

nom*_*ons 6 c

如何在不使用这些运算符的情况下将数字除以未知数('*', '/', '%').分母在运行时给出.

小智 14

void main(){
   int a,b,i=0;
   clrscr();
   printf("Enter the dividend and divisor");
   scanf("%d%d",&a,&b);
   while(a>=b){
      a=a-b;
      i++;
   }

   printf("qoutient is :%d \n remainder : %d",i,a);
   getch();
}
Run Code Online (Sandbox Code Playgroud)

  • 虽然代码可以说明问题,但仅代码的答案并不能提供足够的信息来被视为高质量.请添加有关您的代码解决问题的原因的详细信息. (5认同)

vin*_*wsm 7

您可以使用此功能

int divide(int nu, int de) {

    int temp = 1;
    int quotient = 0;

    while (de <= nu) {
        de <<= 1;
        temp <<= 1;
    }

    //printf("%d %d\n",de,temp,nu);
    while (temp > 1) {
        de >>= 1;
        temp >>= 1;

        if (nu >= de) {
            nu -= de;
            //printf("%d %d\n",quotient,temp);
            quotient += temp;
        }
    }

    return quotient;
}
Run Code Online (Sandbox Code Playgroud)

您可以将分子和分母传递给此函数并获得所需的商.

  • 你正在使用`-`操作符... (2认同)