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>

usingnamespacestd;

boolhas4(intx);

// Returns sum of all digits in numbers from 1 to n

intcountNumbersWith4(intn)

{

intresult = 0; // initialize result

// One by one compute sum of digits in every number from

// 1 to n

for(intx=1; x<=n; x++)

result += has4(x)? 1 : 0;

returnresult;

}

// A utility function to compute sum of digits in a

// given number x

boolhas4(intx)

{

while(x != 0)

{

if(x%10 == 4)

returntrue;

x = x /10;

}

returnfalse;

}

// Driver Program

intmain()

{

intn = 328;

cout << "Count of numbers from 1 to "<< n

<< " that have 4 as a a digit is "

<< countNumbersWith4(n) << endl;

return0;

}

chevron_right

filter_none

Java

filter_none

edit close

play_arrow

link brightness_4 code

// Java program to compute sum of

// digits in numbers from 1 to n

importjava.io.*;

classGFG {

// Returns sum of all digits

// in numbers from 1 to n

staticintcountNumbersWith4(intn)

{

// initialize result

intresult = 0;

// One by one compute sum of digits

// in every number from 1 to n

for(intx=1; x<=n; x++)

result += has4(x)? 1: 0;

returnresult;

}

// A utility function to compute sum

// of digits in a given number x

staticbooleanhas4(intx)

{

while(x != 0)

{

if(x%10== 4)

returntrue;

x = x /10;

}

returnfalse;

}

// Driver Program

publicstaticvoidmain(String args[])

{

intn = 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

filter_none

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

defcountNumbersWith4(n) :

result =0# initialize result

# One by one compute sum of digits

# in every number from 1 to n

forx inrange(1, n +1) :

if(has4(x) ==True) :

result =result +1

returnresult

# A utility function to compute sum

# of digits in a given number x

defhas4(x) :

while(x !=0) :

if(x%10==4) :

returnTrue

x =x //10

returnFalse

# 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

filter_none

C#

filter_none

edit close

play_arrow

link brightness_4 code

// C# program to compute sum of

// digits in numbers from 1 to n

usingSystem;

publicclassGFG

{

// Returns sum of all digits

// in numbers from 1 to n

staticintcountNumbersWith4(intn)

{

// initialize result

intresult = 0;

// One by one compute sum of digits

// in every number from 1 to n

for(intx = 1; x <= n; x++)

result += has4(x) ? 1 : 0;

returnresult;

}

// A utility function to compute sum

// of digits in a given number x

staticboolhas4(intx)

{

while(x != 0)

{

if(x % 10 == 4)

returntrue;

x = x / 10;

}

returnfalse;

}

// Driver Code

publicstaticvoidMain()

{

intn = 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

filter_none

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

functioncountNumbersWith4($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

functionhas4($x)

{

while($x!= 0)

{

if($x% 10 == 4)

returntrue;

$x= intval($x/ 10);

}

returnfalse;

}

// 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

filter_none

Output :

Count of numbers from 1 to 328 that have 4 as a a digit is 60

Efficient Solution:
Above is a naive solution. We can do it more efficiently by finding a pattern.
Let us take few examples.

Count of numbers from 0 to 9 = 1
Count of numbers from 0 to 99 = 1*9 + 10 = 19
Count of numbers from 0 to 999 = 19*9 + 100 = 271
In general, we can write
count(10^{d}) = 9 * count(10^{d - 1}) + 10^{d - 1}

In below implementation, the above formula is implemented using dynamic programming as there are overlapping subproblems.
The above formula is one core step of the idea. Below is complete algorithm.

1) Find number of digits minus one in n. Let this value be 'd'.
For 328, d is 2.
2) Compute some of digits in numbers from 1 to 10^{d} - 1.
Let this sum be w. For 328, we compute sum of digits from 1 to
99 using above formula.
3) Find Most significant digit (msd) in n. For 328, msd is 3.
4.a) If MSD is 4. For example if n = 428, then count of
numbers is sum of following.
1) Count of numbers from 1 to 399
2) Count of numbers from 400 to 428 which is 29.
4.b) IF MSD > 4. For example if n is 728, then count of
numbers is sum of following.
1) Count of numbers from 1 to 399 and count of numbers
from 500 to 699, i.e., "a[2] * 6"
2) Count of numbers from 400 to 499, i.e. 100
3) Count of numbers from 700 to 728, recur for 28
4.c) IF MSD < 4. For example if n is 328, then count of
numbers is sum of following.
1) Count of numbers from 1 to 299 a
2) Count of numbers from 300 to 328, recur for 28

Below is implementation of above algorithm.

C++

filter_none

edit close

play_arrow

link brightness_4 code

// C++ program to count numbers having 4 as a digit

#include<bits/stdc++.h>

usingnamespacestd;

// Function to count numbers from 1 to n that have

// 4 as a digit

intcountNumbersWith4(intn)

{

// Base case

if(n < 4)

return0;

// d = number of digits minus one in n. For 328, d is 2

intd = log10(n);

// computing count of numbers from 1 to 10^d-1,

// d=0 a[0] = 0;

// d=1 a[1] = count of numbers from 0 to 9 = 1

// d=2 a[2] = count of numbers from 0 to 99 = a[1]*9 + 10 = 19

// d=3 a[3] = count of numbers from 0 to 999 = a[2]*19 + 100 = 171

int*a = newint[d+1];

a[0] = 0, a[1] = 1;

for(inti=2; i<=d; i++)

a[i] = a[i-1]*9 + ceil(pow(10,i-1));

// Computing 10^d

intp = ceil(pow(10, d));

// Most significant digit (msd) of n,

// For 328, msd is 3 which can be obtained using 328/100

intmsd = n/p;

// If MSD is 4. For example if n = 428, then count of

// numbers is sum of following.

// 1) Count of numbers from 1 to 399

// 2) Count of numbers from 400 to 428 which is 29.

if(msd == 4)

return(msd)*a[d] + (n%p) + 1;

// IF MSD > 4. For example if n is 728, then count of

// numbers is sum of following.

// 1) Count of numbers from 1 to 399 and count of numbers

// from 500 to 699, i.e., "a[2] * 6"

// 2) Count of numbers from 400 to 499, i.e. 100

// 3) Count of numbers from 700 to 728, recur for 28

if(msd > 4)

return(msd-1)*a[d] + p + countNumbersWith4(n%p);

// IF MSD < 4. For example if n is 328, then count of

// numbers is sum of following.

// 1) Count of numbers from 1 to 299 a

// 2) Count of numbers from 300 to 328, recur for 28

return(msd)*a[d] + countNumbersWith4(n%p);

}

// Driver Program

intmain()

{

intn = 328;

cout << "Count of numbers from 1 to "<< n

<< " that have 4 as a a digit is "

<< countNumbersWith4(n) << endl;

return0;

}

chevron_right

filter_none

Output:

Count of numbers from 1 to 328 that have 4 as a a digit is 60

This article is contributed by Shivam. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above