javaalgorithmdata-structures

Finger Counting Problem to find the name of the finger based on the given number


A boy starts counting using her left hand’s fingers. The counting sequence is like 1-thumb, 2-index finger, 3-middle finger, 4-ring finger, 5-little finger then in the reverse order like 6-ring finger, 7-middle finger, 8-index finger, 9 thumb. Then again 10-index finger and so on. The program must print the last finger used for counting for a given value of N. The fingers names are thumb, index, middle, ring and little.

I found the answer for thumb finger, index finger and the little finger. But I couldnt find the patter for the index and the ring finger. below is the code that i have tried.

import java.util.*;
public class Hello {

    public static void main(String[] args) {
        //Your Code Here
        Scanner sc=new Scanner(System.in);
        long n=sc.nextInt();
        
        if(n%8 == 1) System.out.print("thumb");
        else if(n%8 == 5) System.out.print("little");
        else if(n%4 == 3) System.out.print("middle");
        else {
          // logic for index and ring finger.
        }

    }
}

someone please help me to finish the logic.


Solution

  • As mentioned by @dawood-ibn-kareem to use ||; you can use it something like:

    import java.util.*;
    
    public class Hello {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            long n = sc.nextInt();
            
            if (n % 8 == 1) {
                System.out.print("thumb");
            } else if (n % 8 == 5) {
                System.out.print("little");
            } else if (n % 4 == 3) {
                System.out.print("middle");
            } else if (n % 8 == 2 || n % 8 == 0) {
                System.out.print("index");
            } else if (n % 8 == 4 || n % 8 == 6) {
                System.out.print("ring");
            }
        }
    }