Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 18, 2023 03:41 pm GMT

Challenge: Counting Numbers with 7s

Prompt

Write a program to calculate the percentage of numbers in a given range that contain the digit 7. Your program should be able to calculate the percentages for the following ranges:

  • 0-99
  • 0-999
  • 0-9999
  • 0-99999
  • etc.

The program should take a single integer input n that represents the maximum range (exclusive), i.e., the program will calculate the percentage of numbers containing a 7 in the range from 0 to n-1.

You may use any programming language of your choice. Extra imaginary bonus points for a performant approach that can handle larger and larger ranges efficiently.

Examples

Input => Output

10 => 10.00%100 => 19.00%

In the first example, 1/10 numbers (10%) contain a 7 (just the number 7).

In the second example, 19/100 numbers (19%) contain a 7 (10 numbers from 70 to 79, plus 7, 17, 27, 37, 47, 57, 67, 77, 87, 97). Thus, the output is "19.00%".

Have fun!

7 7 7

Cover image via Unsplash.


Original Link: https://dev.to/peter/challenge-counting-numbers-with-7s-33c7

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To