- Related Questions & Answers
- Count n digit numbers not having a particular digit in C++
- Count all possible N digit numbers that satisfy the given condition in C++
- Count divisors of n that have at-least one digit common with n in Java
- Find count of Almost Prime numbers from 1 to N in C++
- Count n digit numbers divisible by given number in C++
- Count the numbers < N which have equal number of divisors as K in C++
- Maximum XOR using K numbers from 1 to n in C++
- Sum of the Series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + ... in C++\n
- Program to find sum of series 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2) in C++
- C++ Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! + …… n/n!
- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n in C++
- Count ‘d’ digit positive integers with 0 as a digit in C++
- C Program to print numbers from 1 to N without using semicolon
- Program to find Sum of a Series a^1/1! + a^2/2! + a^3/3! + a^4/4! +…….+ a^n/n! in C++
- Count of m digit integers that are divisible by an integer n in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this tutorial, we will be discussing a program to find the numbers from 1 to n that have 4 as a digit.

For this we will be provided with a number n. Our task is to count all the numbers which have 4 as one of their digits and print it out.

#include<iostream> using namespace std; bool has4(int x); //returning sum of digits in the given numbers int get_4(int n){ int result = 0; //calculating the sum of each digit for (int x=1; x<=n; x++) result += has4(x)? 1 : 0; return result; } //checking if 4 is present as a digit bool has4(int x) { while (x != 0) { if (x%10 == 4) return true; x = x /10; } return false; } int main(){ int n = 328; cout << "Count of numbers from 1 to " << n << " that have 4 as a digit is " << get_4(n) << endl; return 0; }

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

Advertisements