- 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

# Find the first natural number whose factorial is divisible by x in C++

We have to find the first natural number whose factorial is divisible by x. The x is given by the user. So if the x = 16, then output will be 6. as 6! mod 16 = 0. We will use general approach to solve this problem. iteratively count 1!, 2!, …. n! and check divisibility using x. If modulus is 0, then stop and return the number.

## Example

#include<iostream> using namespace std; int getNumber(int x) { int fact = 1; int i = 0; while(fact % x != 0){ i++; fact = fact * i; } return i; } int main() { int x = 16; cout << "Minimum value of N is: " << getNumber(x); }

## Output

Minimum value of N is: 6

- Related Questions & Answers
- PHP program to find the first natural number whose factorial can be divided by a number ‘x’
- Number of pairs from the first N natural numbers whose sum is divisible by K in C++
- Sum of first N natural numbers which are divisible by X or Y
- PHP program to find the sum of first n natural numbers that are divisible by a number ‘x’ or a number ‘y’
- Program to find number of pairs from N natural numbers whose sum values are divisible by k in Python
- Program to find number of consecutive subsequences whose sum is divisible by k in Python
- Check if product of first N natural numbers is divisible by their sum in Python
- Smallest number that is divisible by first n numbers in JavaScript
- Largest K digit number divisible by X in C++
- Find Nth positive number whose digital root is X in C++
- C# Program to find whether the Number is Divisible by 2
- Count subarrays whose product is divisible by k in C++
- C++ Program for the Largest K digit number divisible by X?
- How to find whether the Number is Divisible by 2 using C#?
- C++ program to find largest or equal number of A whose sum of digits is divisible by 4

Advertisements