- 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 factorial numbers in a given range in C++

We are given the range starting from an integer value holded by a variable let’s say start till the variable end and the task is to count the total number of factorial numbers available in the given range.

## What is a factorial number

Factorial of a number is calculated by multiplying the digits in a number while decrementing thevalue of digit by 1. It is denoted by the symbol ‘!’ i.e. 0!, 1!, 2!, 3!, 5!,....,etc. Factorial of 0! and 1! is always 1.

I.e. factorial of 2 = 2 * (2-1) = 2 * 1 = 2 factorial of 3 = 3 * (3-1) * (2-1) = 3 * 2 * 1 = 6

## For Example

Input− start = 5, end = 600Output− Count of factorial numbers are 3

**Explanation **− Since, there are 3 numbers available that have factorial numbers in the range 5-600.

Input− start = 1, end = 100Output− Count of factorial numbers are 5

**Explanation** − Since, there are 5 numbers available that have factorial numbers in the range 5-600.

## Approach used in the below program is as follows

Input the range and store in the variables start and end

Take another variable, ‘fact’ to store the factorial values and initialise it with 1 and a temporary variable, ‘i’ to increase the numbers count.

Start the loop, while fact is less than start and keep multiplying the fact with i to calculate the factorial and also, keep incrementing the value of i

Start another loop, while fact is less than equals to end variable and keep incrementing the value of a variable r and keep setting fact to fact *i and keep incrementing value of i

Now, return the value of r that is holding the total count of total number of factorial numbers

Print the result.

## Example

#include <iostream> using namespace std; // To count the number of factorials int factorials(int start, int end){ // Starting from 1 and find the first factorial number // 'fact' greater than or equal to 'start' int fact = 1, i = 1; while (fact < start){ fact = fact*i; i++; } // r to count factorial numbers in range start to end int r = 0; while (fact <= end){ r++; fact = fact*i; i++; } // Return the count of factorials in range return r; } int main(){ int start = 5, end = 600; cout << "Count of factorial numbers are " << factorials(start, end); return 0; }

## Output

If we run the above code it will generate the following output −

Count of factorial numbers are 3

- Related Articles
- Count the numbers divisible by ‘M’ in a given range in C++
- Count numbers with unit digit k in given range in C++
- Count Unary Numbers in a Range in C++
- Count digits in a factorial in C++
- Count BST nodes that lie in a given range in C++
- Count of common multiples of two numbers in a range in C++
- Program to find count of numbers having odd number of divisors in given range in C++
- Count Fibonacci numbers in given range in O(Log n) time and O(1) space in C++
- Count number of smallest elements in given range in C++
- Count BST subtrees that lie in given range in C++
- Print prime numbers in a given range using C++ STL
- Count total divisors of A or B in a given range in C++
- Print all Good numbers in given range in C++
- Count trailing zeros in factorial of a number in C++
- Find numbers with K odd divisors in a given range in C++