CrazyEngineers
  • Dividing two numbers

    Sachin Jain

    Member

    Updated: Oct 26, 2024
    Views: 1.0K
    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
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • srinivas_j

    MemberJan 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!!)
    Are you sure? This action cannot be undone.
    Cancel
  • Sachin Jain

    MemberJan 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
    Are you sure? This action cannot be undone.
    Cancel
  • silverscorpion

    MemberJan 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!! 😀
    Are you sure? This action cannot be undone.
    Cancel
  • srinivas_j

    MemberJan 11, 2011

    no!! it's not inside d while loop!! it's outside itself try it out by Executing!!
    Are you sure? This action cannot be undone.
    Cancel
  • prabait28

    MemberJan 12, 2011

    Tell me Any other crct answer with simple concept.....
    Are you sure? This action cannot be undone.
    Cancel
  • srinivas_j

    MemberJan 13, 2011

    it's d simplest one & it takes very less cpu cycle time compared to ur other method!! & mor over logic is simple!
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register