VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-count-numbers-fingers/

⇱ Program to Count numbers on fingers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to Count numbers on fingers

Last Updated : 31 Mar, 2023

Count the given numbers on your fingers and find the correct finger on which the number ends. 
 


 

👁 Image


Examples: 
 

Input : 17
Output :1

Input :27
Output :3


 

Recommended Practice

Approach: 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 numbers
    • 1 to 8
    • 9 to 16
    • 17 to 24, and so on
       

Output
4

Time Complexity: O(1), As we are doing only constant time operations.
Auxiliary Space: O(1), As constant extra space is used.


Asked in Paytm Campus Placement August 2017 

#Example:2


Output
Total number of fingers used to count 23 is 23

Approach:

1.Define a function count_fingers that takes an integer n as input. The function will return the total number of fingers used to count up to n.
2.Calculate the number of full hands needed to count up to n by dividing n by 10 and using integer division to round down to the nearest whole number. Assign this value to the variable full_hands.
3.Calculate the number of fingers used in the full hands by multiplying full_hands by 10. Assign this value to the variable full_fingers.
4.Calculate the number of remaining fingers needed to count up to n after using all full hands by taking the remainder of n divided by 10. 5.Assign this value to the variable remaining_fingers.
6.Calculate the total number of fingers used to count up to n by adding full_fingers and remaining_fingers. Assign this value to the variable total_fingers.
7.Return the total_fingers value.
8.In the example usage, create an integer n and call the count_fingers function with this argument. Finally, print the total number of fingers used to count up to n.

Time complexity: O(1).

Space complexity: O(1).


 

Comment