Program to Count numbers on fingers

15 Jun, 2021
Count the given numbers on your fingers and find the correct finger on which the number ends.  
The first number starts from the thumb, second on the index finger, third on the middle finger, fourth on the ring finger, and fifth on the little finger.
Again six comes on the ring finger and so on.
Here we observer a pattern, 8(last number) and 2 ends up in 2nd position, 3rd or 7th on the middle finger, and so on.
The pattern keeps repeating after every 8 numbers1 to 89 to 1617 to 24, and so on
1 to 8
9 to 16
17 to 24, and so on
 
Examples:  
Input : 17
Output :1

Input :27
Output :3
 
 
C++
Java
Python3
C#
PHP
Javascript
// CPP Program to Count numbers on fingers#include <iostream>using namespace std; int count_num_finger(int n){    int r = n % 8;    if (r == 0)        return 2;    if (r < 5)        return r;    else        return 10 - r;} // Driver codeint main(){    int n;    n = 30;    cout << count_num_finger(n);     return 0;}
// Java Program to Count numbers on fingersclass GFG{static int count_num_finger(int n){    int r = n % 8;    if (r == 0)        return 2;    if (r < 5)        return r;    else        return 10 - r;} // Driver Codepublic static void main(String[] args){    int n;    n = 30;    System.out.println(count_num_finger(n));}} // This code is contributed// by Mukul Singh
def count_num_finger( n ):    r = n % 8    if r == 0:        return 2    if r < 5:        return r    else:        return 10 - r # Driver Coden = 30print(count_num_finger(n)) # This code is contributed by "Sharad_Bhardwaj".
// C# Program to Count numbers on fingersusing System;     class GFG{     static int count_num_finger(int n){    int r = n % 8;    if (r == 0)        return 2;    if (r < 5)        return r;    else        return 10 - r;} // Driver Codepublic static void Main(String[] args){    int n;    n = 30;    Console.WriteLine(count_num_finger(n));}} // This code is contributed by Princi Singh
<?phpfunction count_num_finger( $n ){    $r = $n % 8;    if ($r == 2)        return 0;    if ($r < 5)        return $r;    else        return 10 - $r;}     // Driver Code$n = 30;echo(count_num_finger($n)); // This code is contributed// by Aman Ojha?>
<script>    // Javascript Program to Count numbers on fingers         function count_num_finger(n)    {        let r = n % 8;        if (r == 0)            return 2;        if (r < 5)            return r;        else            return 10 - r;    }         let n;    n = 30;    document.write(count_num_finger(n));  // This code is contributed by mukesh07.</script>
Output:  
4
Asked in Paytm Campus Placement August 2017 This article is contributed by Dinesh Malav. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Aman ojha
Code_Mech
princi singh
AyushGupta31
mukesh07
logical-thinking
Paytm
School Programming
Paytm
logical-thinking
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.