
- 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
Number of n digit stepping numbers in C++
The stepping number is a number where the difference between the consecutive digits is 1. You are given a number n representing the number of digits. You need to count the total number of stepping numbers with n digits.
Let's see an example.
Input
2
Output
17
The lowest number of 2 digits is 10 and highest number of 2 digits is 99. There are 17 stepping numbers in between them.
Algorithm
- Initialise the number n.
- Initialise the count to 0.
- Find the lowest number of n digits i.e.., pow(10, n - 1).
- Find the highest number of n digits i.e.., pow(10, n) - 1.
- Write a loop that iterates from the lowest n digit number to highest n digit number.
- Check whether the current number is stepping number or not.
- Check the difference between consecutive pair of digits in the number.
- If the result of the any difference is not 1, then return false else true.
- Increment the count if the current number is a stepping number.
- Return the count.
Implementation
Following is the implementation of the above algorithm in C++
#include <bits/stdc++.h> using namespace std; bool isSteppingNumber(int n) { int previousDigit = -1; while (n) { int currentDigit = n % 10; if (previousDigit != -1 && abs(previousDigit - currentDigit) != 1) { return false; } previousDigit = currentDigit; n /= 10; } return true; } int getSteppingNumbersCount(int n) { int lowestNumber = pow(10, n - 1), highestNumber = pow(10, n) - 1; int count = 0; for (int i = lowestNumber; i <= highestNumber; i++) { if (isSteppingNumber(i)) { count += 1; } } return count; } int main() { int n = 3; cout << getSteppingNumbersCount(n) << endl; return 0; }
Output
If you run the above code, then you will get the following result.
32
- Related Articles
- Program to count number of stepping numbers of n digits in python
- Stepping Numbers in C++
- Count n digit numbers divisible by given number in C++
- Largest N digit number divisible by given three numbers in C++
- Count n digit numbers not having a particular digit in C++
- C++ program to count minimum number of binary digit numbers needed to represent n
- Count of Binary Digit numbers smaller than N in C++
- Find the largest palindrome number made from the product of two n digit numbers in JavaScript
- Largest number less than N with digit sum greater than the digit sum of N in C++
- Print all n-digit strictly increasing numbers in C++
- Numbers At Most N Given Digit Set in C++
- Largest Even and Odd N-digit numbers in C++
- Counting n digit Numbers with all unique digits in JavaScript
- Count of Numbers in Range where first digit is equal to last digit of the number in C++
- Count of all N digit numbers such that num + Rev(num) = 10^N - 1 in C++

Advertisements