Count numbers from 1 to n that have 4 as a digit

Given a number n, find count of all numbers from 1 to n that have 4 as a digit.

Examples :

Input:   n = 5
Output:  1
Only 4 has '4' as digit

Input:   n = 50
Output:  14

Input:   n = 328
Output:  60

This problem is mainly a variation of previous article on Compute sum of digits in all numbers from 1 to n.

Naive Solution:
A naive solution is to go through every number x from 1 to n, and check if x has 4. To check if x has or not, we can traverse all digits of x. Below is the implementation of above idea :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// A Simple C++ program to compute sum of digits in numbers from 1 to n
#include<iostream>
using namespace std;
  
bool has4(int x);
  
// Returns sum of all digits in numbers from 1 to n
int countNumbersWith4(int n)
{
    int result = 0; // initialize result
  
    // One by one compute sum of digits in every number from
    // 1 to n
    for (int x=1; x<=n; x++)
        result += has4(x)? 1 : 0;
  
    return result;
}
  
// A utility function to compute sum of digits in a
// given number x
bool has4(int x)
{
    while (x != 0)
    {
        if (x%10 == 4)
           return true;
        x   = x /10;
    }
    return false;
}
  
// Driver Program
int main()
{
   int n = 328;
   cout << "Count of numbers from 1 to " << n 
        << " that have 4 as a a digit is " 
        << countNumbersWith4(n) << endl;
   return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to compute sum of
// digits in numbers from 1 to n
import java.io.*;
  
class GFG {
      
    // Returns sum of all digits
    // in numbers from 1 to n
    static int countNumbersWith4(int n)
    {
        // initialize result
        int result = 0;
       
        // One by one compute sum of digits
        // in every number from 1 to n
        for (int x=1; x<=n; x++)
            result += has4(x)? 1 : 0;
       
        return result;
    }
      
    // A utility function to compute sum
    // of digits in a given number x
    static boolean has4(int x)
    {
        while (x != 0)
        {
            if (x%10 == 4)
               return true;
            x   = x /10;
        }
        return false;
    }
       
    // Driver Program
    public static void main(String args[])
    {
       int n = 328;
       System.out.println("Count of numbers from 1 to "
                          + " that have 4 as a a digit is "
                          + countNumbersWith4(n)) ;
    }
}
  
// This code is contributed by Nikita Tiwari.

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# A Simple Python 3 program to compute
# sum of digits in numbers from 1 to n
  
# Returns sum of all digits in numbers from 1 to n
def countNumbersWith4(n) :
    result = 0 # initialize result
  
    # One by one compute sum of digits
    # in every number from 1 to n
    for x in range(1, n + 1) :
        if(has4(x) == True) :
            result = result + 1
  
    return result
  
# A utility function to compute sum  
# of digits in a given number x
def has4(x) :
    while (x != 0) :
        if (x%10 == 4) :
            return True
        x = x //10
      
    return False
      
# Driver Program
n = 328
print ("Count of numbers from 1 to ", n,
        " that have 4 as a a digit is "
                    countNumbersWith4(n)) 
  
  
# This code is contributed by Nikita Tiwari.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to compute sum of
// digits in numbers from 1 to n
using System;
  
public class GFG 
{
      
    // Returns sum of all digits
    // in numbers from 1 to n
    static int countNumbersWith4(int n)
    {
          
        // initialize result
        int result = 0;
      
        // One by one compute sum of digits
        // in every number from 1 to n
        for (int x = 1; x <= n; x++)
            result += has4(x) ? 1 : 0;
      
        return result;
    }
      
    // A utility function to compute sum
    // of digits in a given number x
    static bool has4(int x)
    {
        while (x != 0)
        {
            if (x % 10 == 4)
            return true;
            x = x / 10;
        }
        return false;
    }
      
    // Driver Code
    public static void Main()
    {
        int n = 328;
        Console.WriteLine("Count of numbers from 1 to "
                        + " that have 4 as a a digit is "
                        + countNumbersWith4(n)) ;
    }
}
  
// This code is contributed by Sam007 

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to compute sum of
// digits in numbers from 1 to n
  
// Returns sum of all digits 
// in numbers from 1 to n
function countNumbersWith4($n)
{
    $result = 0; // initialize result
  
    // One by one compute sum of 
    // digits in every number from 1 to n
    for ($x = 1; $x <= $n; $x++)
        $result += has4($x) ? 1 : 0;
  
    return $result;
}
  
// A utility function to compute 
// sum of digits in a given number x
function has4($x)
{
    while ($x != 0)
    {
        if ($x % 10 == 4)
        return true;
        $x = intval($x / 10);
    }
    return false;
}
  
// Driver Code
$n = 328;
echo "Count of numbers from 1 to " . $n
        " that have 4 as a a digit is " .
                   countNumbersWith4($n);
      
// This code is contributed by Sam007
?>

chevron_right