Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What should be code(any language) for the following program.?

There are two people, A and B. Each has an integer. Together, the two integers multiply to either8 or. A: "With the number I have, I do not know the number you have." B: "I still don’t know what you have either." A: "I know what you have now." B: "Oh mate, I know what you have too." What are the numbers that B could have?

user-image
Question added by Abdul Khader Shaik , IT & Ops Manager , TheSmartlabs
Date Posted: 2017/12/31

lets discuss the the possibilities

if "A" have the number 1 then "B" will have 8 or 16 and the multiply will be either 8 or 16, so "A" dont know what is the number of "B" 8 or 16

if "A" have the number 2 the possibilities for "B" is 4 or 8if "A" have the number 4 the possibilities for "B" is 2 or 4if "A" have the number 8 the possibilities for "B" is 1 or 2if "A" have the number 16 the possibility for "B" is just 1

how we will do it by programming:we will make a counter start with 0 to know the number of the possibilities of "B"then we will check the counter value.the value will changed if the multiply is correct.if the value was 1 we will print the value of "A" and "B"else the value should be 2 and we will do nothing.

Here is the code in C++ language

////////////////////////////////////////#include <iostream>using namespace std;void main(){    int Count;    for (int a=1;a<=16;a++){        Count=0;        for (int b=1;b<=16; b++){            if (a*b==16||a*b==8){            Count++;            }        }        if (Count ==1){            cout <<"know"<<endl;            for (int b=1;b<=16; b++){            if (a*b==16||a*b==8)cout <<a<<"\\t"<<b<<endl;            }        }        else cout<<"dont know"<<endl;    }}///////////////////////////////////

I put two strings ("know", "dont know") to know the condition if it was true or false

if any one have another way to solve I am interested to know it.

More Questions Like This