
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- 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++
- Program to find out the number of special numbers in a given range in Python
- C++ code to find total elements in a matrix
- C++ code to find out the sum of the special matrix elements
- Finding number of alphabets, digits and special characters in strings using C language
- C++ code to find minimum different digits to represent n
- Program to count number of stepping numbers of n digits in python
- Find smallest number with given number of digits and sum of digits in C++
- Find the number of whole numbers between the smallest and the greatest number of 2 digits.
- Fetch Numbers with Even Number of Digits JavaScript
- C++ code to find out the total amount of sales we made
- Java program to find the percentage of uppercase, lowercase, digits and special characters in a String

Advertisements