Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Problem involving 3 digit numbers

  1. Jan 27, 2014 #1

    I am writing a program where program will randomly generate a three digit number from 100 to 999 and then user will be asked to guess that number. If the number is matched exactly, then the highest prize is given to the user, if all digits in a guess are also in the original number, next highest prize will be given to the user. If two digits from guess are correct, then the next prize . If one digit from the guess is correct, then the next one. Finally no prize for the guess where no digits match.
    I tried to use if-else construct for this. I am able to get the first two correct. But I am having problems with logic where guess might have two of its digits correct and where guess might have only single digit correct. Any specific ideas ?

  2. jcsd
  3. Jan 27, 2014 #2
    Store the digits in an array. Loop through the array and count the matches.
    for(nR=0;nR<3;nR++) naTemp[nR] = naRandom[nR]
    for(nG=0;nG<3;nG++) {
    nGD = naGD[nG];
    for(nR=0;nR<3;nR++) {
    if(nDG==naTemp[nR]) {
    naTemp[nR] = -1;
  4. Jan 27, 2014 #3
    thanks Scott.... makes sense
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook