
- 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
Count numbers with unit digit k in given range in C++
We are given an interval [first,last]. The goal is to find the count of numbers that have a unit digit k and lie between range [first,last].
We will do this by traversing from i=first to i=last. For each number i compare its unit digit with the k, if they are the same increment the count.
Let’s understand with examples.
Input − first=8 last=40 , k=8
Output − Count of numbers with unit digit k − 4
Explanation −
Numbers between 8 and 40 with unit digit = 8 8,18, 28, 38
Input − first=100 last=200 , k=9
Output − Count of numbers with unit digit k − 10
Explanation −
Numbers between 100 and 200 with unit digit = 9 109, 119, 129, 139, 149, 159, 169, 179, 189, 199. Total:10
Approach used in the below program is as follows
We take two integers first and last to define range [first, last].
Function getCount(int fst, int lst, int k) takes range variables and k and returns the count of numbers between fst and lst and have unit digit as k.
Take the initial count as 0.
Using for loop start from i=fst to i=lst, for each i calculate unit digit as ldigit=i%10.
If ldigit==k, increment count.
Return count as result.
Example
#include <bits/stdc++.h> using namespace std; int getCount(int fst,int lst,int k){ int count=0; for(int i=fst;i<=lst;i++){ int ldigit=i%10; //to get last digit if(ldigit==k) //if both are equal increment count { ++count; } } return count; } int main(){ int first = 5, last = 30; int K=5; cout<<"Numbers with unit digit K in range:"<<getCount(first, last, K); return 0; }
Output
If we run the above code it will generate the following output −
Numbers with unit digit K in range:3
- Related Articles
- Count of Numbers in a Range where digit d occurs exactly K times in C++
- Digit Count in Range
- Find numbers with K odd divisors in a given range in C++
- Count all the numbers in a range with smallest factor as K in C++
- Count factorial numbers in a given range in C++
- C++ program to find numbers with K odd divisors in a given range
- Count n digit numbers divisible by given number in C++
- Count numbers (smaller than or equal to N) with given digit sum in C++
- Count of Numbers in Range where first digit is equal to last digit of the number in C++
- Count the numbers divisible by ‘M’ in a given range in C++
- Create list of numbers with given range in Python
- Count all possible N digit numbers that satisfy the given condition in C++
- Count of Numbers in a Range divisible by m and having digit d in even positions in C++
- Finding sequential digit numbers within a range in JavaScript
- Count n digit numbers not having a particular digit in C++
