

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ code to find total number of digits in special numbers
Suppose, we are given an integer number k. We call a number special number if all the digits in that number are the same. For example, 1, 11, 1111 are special numbers. We count the special numbers in order 1, 11, 111, 1111, 2, 22, 222, 2222, 3, 33, 333, 3333, and so on. We have to find out the total number of digits that are in special numbers up to k. The value of k is not greater than 10000.
So, if the input is like k = 9999, then the output will be 90.
Steps
To solve this, we will follow these steps −
s := convert k to string Define an array v of size: := {0, 1, 3, 6, 10} print(((s[0] - '0') - 1) * 10 + v[length of s])
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; #define N 100 void solve(int k) { string s = to_string(k); int v[] = {0, 1, 3, 6, 10}; cout<< ((s[0] - '0') - 1) * 10 + v[s.length()] << endl; } int main() { int k = 9999; solve(k); return 0; }
Input
9999
Output
90
- Related Questions & Answers
- Total number of non-decreasing numbers with n digits
- C++ code to count number of lucky numbers with k digits
- Find Numbers with Even Number of Digits in Python
- Count total number of digits from 1 to N in C++
- C++ code to find total elements in a matrix
- Program to find out the number of special numbers in a given range in Python
- C++ code to find out the sum of the special matrix elements
- C++ code to find out the total amount of sales we made
- Find smallest number with given number of digits and sum of digits in C++
- Fetch Numbers with Even Number of Digits JavaScript
- Finding number of alphabets, digits and special characters in strings using C language
- Program to count number of stepping numbers of n digits in python
- Find the Largest number with given number of digits and sum of digits in C++
- C++ code to find minimum different digits to represent n
- C++ code to find total time to pull the box by rabbit
Advertisements