Dividing two numbers

Sachin Jain

Sachin Jain

@sachin-0wuUmc Oct 26, 2024
You are given two numbers A and B,
You have to divide A by B without using division operator and modulus operator ?

PS: Repeated Subtraction is not allowed..

I have thought of a solution but i am not satisfied with that so asking for help of CEans

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • srinivas_j

    srinivas_j

    @srinivas-j-XNEHhO Jan 9, 2011

    hi!! dude the following code serves your purpose!!
    #include<stdio.h>
    void main()
    {
    float a,b,r=0;
    printf("\n\n\tEnter Values of a & b :");
    scanf("%d%d",&a,&b);
    while(b*r <= a)
    r++;
    printf("\n\n\tAnswer is : %d ",r-1);
    }

    Have a Nice time!!(Note : It's for Integers!! ie, i've not taken floating point division to easy the task!!)
  • Sachin Jain

    Sachin Jain

    @sachin-0wuUmc Jan 9, 2011

    Thats OK...Actually its a YAHOO interview question.
    I thought of doing like this :-
    We have to calculate y=A/B
    So take log of both sides
    log y=log(A/B)
    log y=log A - log B
    calculate log A - log B=x say
    Then take antilog both sides
    y=antilog (x)

    In this way we can find A/b without using / and % operator.
    Thats what i thought
  • silverscorpion

    silverscorpion

    @silverscorpion-iJKtdQ Jan 9, 2011

    srinivas_j
    hi!! dude the following code serves your purpose!!
    #include<stdio.h>
    void main()
    {
    float a,b,r=0;
    printf("\n\n\tEnter Values of a & b :");
    scanf("%d%d",&a,&b);
    while(b*r <= a)
    r++;
    printf("\n\n\tAnswer is : %d ",r-1);
    }

    Have a Nice time!!(Note : It's for Integers!! ie, i've not taken floating point division to easy the task!!)
    Here, I think the printf statement should be outside the while loop!! 😀
  • srinivas_j

    srinivas_j

    @srinivas-j-XNEHhO Jan 11, 2011

    no!! it's not inside d while loop!! it's outside itself try it out by Executing!!
  • prabait28

    prabait28

    @prabait28-OKNXt2 Jan 12, 2011

    Tell me Any other crct answer with simple concept.....
  • srinivas_j

    srinivas_j

    @srinivas-j-XNEHhO Jan 13, 2011

    it's d simplest one & it takes very less cpu cycle time compared to ur other method!! & mor over logic is simple!